704. 二分查找
class Solution {
public:
int search(vector<int>& nums, int target) {
int left =0;
int right = nums.size()-1;
while(left<=right){
int middle = left + floor((right-left)/2);
if(nums[middle]==target) return middle;
if(nums[middle]< target){
left = middle+1;
}
if(nums[middle]>target){
right = middle-1;
}
}
return -1;
}
};
27. 移除元素
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int size =nums.size();
int slow =0;
for(int fast=0; fast<size;fast++){
if(nums[fast] != val){
nums[slow] =nums[fast];
slow++;
}
}
return slow;
}
};