704.Binary Search
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.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
写解答时需要分清楚边界条件如左闭右闭以及左闭右开
非递归法
public int search(int[] nums, int target) {
if(nums.length == 1 && nums[0] != target) return -1;
int left = 0;
int right = nums.length - 1;
// 左闭右闭 left == right 有效 (如果是左闭右开则为 left < right left == right 无效)
while(left <= right){
int mid = (right - left)/2 + left;
if(nums[mid] == target){
return mid;
}else if(nums[mid] > target){
right = mid - 1;
}else if(nums[mid] < target){
left = mid + 1;
}
}
return -1;
}
- 时间复杂度:O(log n)
- 空间复杂度:O(1)
递归法
public int search(int[] nums, int target) {
if(nums.length == 1 && nums[0] != target) return -1;
return binary(nums, 0, nums.length - 1, target);
}
public int binary(int[] nums, int left, int right, int target){
int mid = (right - left)/2 + left; // 等同于(right - left)/2,+ left 是为了防止溢出
if(nums[mid] == target) return mid;
if(left >= right) return -1;
else if(target > nums[mid]){
return binary(nums, mid + 1, right, target);
}
else if (target < nums[mid]){
return binary(nums, left, mid - 1, target);
}
return -1;
}
- 时间复杂度:O(log n)
- 空间复杂度:O(1)
27. Remove Elements
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
numssuch that the firstkelements ofnumscontain the elements which are not equal toval. The remaining elements ofnumsare not important as well as the size ofnums. - Return
k.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.
int k = removeElement(nums, val); // Calls your implementation
assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).
最开始尝试了暴力解法。但本题最好的解法为双指针解法
public int removeElement(int[] nums, int val) {
int post = 0; // 设置慢指针
// 设置快指针
for(int pre = 0; pre < nums.length; pre++){
// 如果快指针和删除值不相同则覆盖慢指针对应的值
if(nums[pre] != val){
nums[post++] = nums[pre];
}
}
return post;
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)