xxxxxxxxxx
class Solution {
public int removeElement(int[] nums, int val) {
int count =0;
int j = 0;
for(int i=0; i<nums.length; i++){
if(nums[i]==val){
count+=1;
continue;
}
nums[j] = nums[i];
j++;
}
return nums.length-count;
}
}
//Remove Element
xxxxxxxxxx
class Solution {
public int[] searchRange(int[] nums, int target) {
int res[] = new int[]{-1, -1};
int start = 0;
int last = nums.length-1;
while(start <= last)
{
int mid = start + (last - start)/2;
if(nums[mid] == target)
{
res[0] = mid;
last = mid - 1;
}
else if(nums[mid] < target)
{
start = mid+1;
}
else
{
last = mid -1;
}
}
start =0;
last = nums.length-1;
while(start <= last)
{
int mid = start + (last - start)/2;
if(nums[mid] == target)
{
res[1] = mid;
start = mid + 1;
}
else if(nums[mid] < target)
{
start = mid+1;
}
else
{
last = mid -1;
}
}
return res;
}
}
xxxxxxxxxx
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head.next == null)
{
return null;
}
int size = 0;
ListNode current = head;
while(current != null)
{
current = current.next;
size++;
}
if(n == size){
return head.next;
}
int indexToSearch = size - n;
ListNode prev = head;
int i = 1;
while(i < indexToSearch)
{
prev = prev.next;
i++;
}
prev.next = prev.next.next;
return head;
}
}
xxxxxxxxxx
class Solution {
public boolean isPalindrome(int x) {
int temp = x;
int dig, sum=0;
while(temp > 0)
{
dig = temp % 10;
sum = sum * 10 + dig;
temp = temp / 10;
}
if(sum == x)
return true;
else
return false;
}
}