704. Binary Search
Link: https://leetcode.com/problems/binary-search/
Reference: https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html
Description
Given an array of integers nums
which is sorted in ascending order, and an integer target
, write a function to search target
in nums
. If target
exists, then return its index. Otherwise, return -1
.
You must write an algorithm with O(log n)
runtime complexity.
Approach
Binary Search
- Initialize the left and right pointer, which corresponds to the start and end of
the pointer. - While
nums[left] < nums[right]
:- Calculate the middle index:
middle = left + (right - left) / 2
. It can avoid the overflow problem. - if
nums[middle] == target
: Return middle. - If
nums[middle] < target
: Setleft = middle + 1
. - If
nums[middle] > target
: Setright = middle - 1
- Calculate the middle index:
- If the target is not found, return -1.
Solutions
class Solution {
public int search(int[] nums, int target) {
int right = nums.length - 1;
int mid = 0;
int left = 0;
if (nums[right] < target || nums[left] > target)
return -1;
while (left <= right) {
//(left + right) / 2 can cause overflow problem
mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
}
class Solution {
public int search(int[] nums, int target) {
int right = nums.length;
int mid = 0;
int left = 0;
if (nums[right - 1] < target || nums[left] > target)
return -1;
while (left < right) {
mid = left + (right - left) / 2;
System.out.println(mid);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid;
}
return -1;
}
}
Remark
Be careful of the boundary.
27. Remove Element
Link: https://leetcode.com/problems/remove-element/
Reference: https://programmercarl.com/0027.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.html
Description
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
Consider the number of elements in nums
which are not equal to val
be k
, to get accepted, you need to do the following things:
- Change the array nums such that the first
k
elements ofnums
contain
the elements which are not equal toval
. The remaining elements of
nums
are not important as well as the size ofnums
. - Return
k
.
Approach
Two pointers
- Initialize a fast pointer and a slow pointer. The fast pointer is used to find element does not equal to
k
. The slow pointer is used to updatenums
. - Iterate over
nums
, check whethernums[fast] = val
- If
nums[fast] != val
, setnums[slow] = nums[fast]
- If
nums[fast] == val
, skip this element slow++
, point slow to the next update position
- If
- Return
slow
, which represents the length of the non-target array.
Solutions
class Solution {
public int removeElement(int[] nums, int val) {
int fast = 0;
int slow = 0;
for (fast = 0; fast < nums.length; fast++) {
if (nums[fast] != val)
nums[slow++] = nums[fast];
}
return slow;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
int right = nums.length - 1;
int left = 0;
while (left <= right) {
while (left <= right && nums[left] != val)
left++;
while (right >= left && nums[right] == val)
right--;
if (left < right)
nums[left++] = nums[right--];
}
return left;
}
}