代码随想录算法训练营Day1 | 704. 二分查找、27. 移除元素。
LeetCode 704 二分查找
题目链接:LeetCode 704 二分查找
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
思路:
设置左右区间,二分法缩小区间。
重点在于区间的开闭
左闭右闭
[left, right)
left=right 有意义
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right= size(nums)-1;
while(left <= right){
int mid = left + (right-left)/2;
if (nums[mid] < target){
left = mid + 1;
}
else if (nums[mid] > target){
right = mid - 1;
}
else{
return mid;
}
}
return -1;
}
};
注意 (right-left)/2
变为 (right-left)>>1
须加括号
左闭右开
[left, right)
left=right 无意义
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right= size(nums);
while(left < right){
int mid = left + ((right-left)>>1);
if (nums[mid] < target){
left = mid + 1;
}
else if (nums[mid] > target){
right = mid;
}
else{
return mid;
}
}
return -1;
}
};
注意:
(right-left)/2
变为(right-left)>>1
须加括号- 左闭右开
right=size(nums)
- 如果left或者right为闭区间,判断时mid需+1或-1
如果为开区间,mid不变,
LeetCode 27 移除元素
题目链接: LeetCode 27 移除元素
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 of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
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.9
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).
思路:
双指针,快慢指针法
当快指针的元素不等于慢指针指向的元素,更新慢指针。
快指针维护元素判断
慢指针维护新数组元素
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int slow = 0;
for (int fast=0; fast < nums.size(); fast++){
if (nums[fast] != val){
nums[slow++] = nums[fast];
}
}
return slow;
}
};
暴力法
代码实现练习
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int size = nums.size();
int i = 0;
for(; i<size; i++)
if(nums[i]==val){
for(int j=i+1;j<size;j++)
nums[j-1]=nums[j];
i--;
size--;
}
return i;
}
};
注意:
- 暴力法注意 size和指针的动态变化