Directory
Basics of Array Theory
- Array index start from
0
- Array is a data collection stored in the contiguous memory space
- Because the array’s memory space is contiguous, we must modify the other element address when deleting or adding elements
- We can’t delete array elements, only overwritten
LeetCode 704. Binary Search
Solution:
class Solution {
// avoid looping when tartget less than nums[0] and bigger than nums[nums.length - 1]
if(target < nums[0] || target > nums[nums.length-1])
return -1;
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
// left-closed and right-closed interval
while(left <= right) {
int middle = (left + right) >> 1;
if(nums[middle] > target)
right = middle - 1;
else if(nums[middle] < target)
left = middle + 1;
else
return middle;
}
return -1;
}
}
Thoughts:
- Avoid looping when
target
less thannums[0]
and bigger thannums[nums.length - 1]
- Two greater than signs
>>
means move one place to the right.>> 1
means divide by 2. - Two less than signs
<<
means move one place to the left.<< 1
means multiple 2. - The prerequisite of using binary search is an
ordered distinct
array. - We have to follow the
loop invariant
principle. It means we should do our boundary operation according to the interval’s definition. - We define target in a
left-closed and right-closed interval
,that is[left, right]
. - We use
left <= right
in the while loop. Becauseleft == right
makes sense.
LeetCode 27. Remove Element
Solution:
class Solution {
public int removeElement(int[] nums, int val) {
int slow = 0;
for(int fast=0; fast < nums.length; fast++){
if(nums[fast] != val){
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}
Time Complexity: O(n)
Space Complexity: O(1)
Thoughts:
- I adopt the
Two Pointers Method
. Complete the work of2 for loop
under1 for loop
through a slow and a fast point. - The principle of solving this two-pointer question is clarifying the definition of these two pointers.
Fast Point
: find elements of the new array that doesn’t contain the targetval
.Slow Point
: update new array index.
LeetCode 35. Search Insert Position
Solution:
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right){
int middle = (left + right) >> 1;
if(nums[middle] == target)
return middle;
else if(nums[middle] > target)
right = middle - 1;
else if(nums[middle] < target)
left = middle + 1;
}
return right + 1;
}
}
Time Complexity:O(log n)
Space Complexity:O(1)
Thoughts:
- If the question says
Given a sorted array of distinct integers
, we can consider whether we could useBinary Search
. - Accoring to
loop invariant
principle. We define target in aleft-closed and right-closed interval
, that is[left, right]
- Insert the target value to the array,there are four situations:
- 1、target value comes before all elements of the array:
[0, -1]
- 2、target value is equal to an element in the array: return middle
- 3、target value is in the array:
[left, right]
,return right + 1 - 4、target value is behind all elements:
[left, right]
:return right + 1;
- 1、target value comes before all elements of the array:
LeetCode 34. First and Last Position of Element in Sorted Array
Solution:
class Solution {
public int[] searchRange(int[] nums, int target) {
int rightBorder = getRightBorder(nums, target);
int leftBorder = getLeftBorder(nums, target);
// Situation one: `target` in the left or right of the array
if (rightBorder == -2 || leftBorder == -2)
return new int[]{-1,-1};
// Situation three: `target` in the array's range, also in the array.
if (rightBorder - leftBorder > 1)
return new int[]{leftBorder + 1, rightBorder - 1};
// Situation two: `target` in the array's range but not in the array.
return new int[]{-1, -1};
}
int getLeftBorder(int[] nums, int target) {
int left = 0, right = nums.length - 1;
int leftBorder = -2;
while(left <= right) {
int middle = (left + right) >> 1;
// update right when nums[middle] == target
if (nums[middle] >= target) {
right = middle - 1;
leftBorder = right;
} else {
left = middle + 1;
}
}
return leftBorder;
}
int getRightBorder(int[] nums, int target) {
int left = 0, right = nums.length-1;
int rightBorder = -2;
while(left <= right) {
int middle = (left + right) >> 1;
if (nums[middle] > target) {
right = middle -1;
}
// update left when nums[middle] == target
else {
left = middle + 1;
rightBorder = left;
}
}
return rightBorder;
}
}
Thoughts:
- There are three situations:
- Situation one:
target
in the left or right of the array. Like an array{3, 4, 5}
, the target is2
or6
. Should return{-1, -1}
- Situation two:
target
in the array’s range but not in the array. Like an array{3, 6, 7}
, the target is5
. Should return{-1, -1}
- Situation three:
target
in the array’s range, also in the array. Like an array{3, 6, 7}
, the target is6
. Should return{1, 1}
- Situation one:
版权声明:本文为kyle313606922原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。