赞
踩
class Solution { public: int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while(left < right){ int mid = (left + right) >> 1; // 可以写成mid = left + (right - left) >> 1 if(nums[mid] >= target){ right = mid; }else{ left = mid + 1; } } if(nums[left] == target){ return left; } return -1; } };
class Solution { public: int searchInsert(vector<int>& nums, int target) { // 找第一个>=target的位置 int left = 0, right = nums.size(); while(left < right){ int mid = (left + right) >> 1; if(nums[mid] >= target){ right = mid; } else{ left = mid + 1; } } return left; } };
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { if(nums.empty()){ return {-1, -1}; } // lower_bound和upper_bound,注意二分计算时mid的溢出 // 1. 第一个位置 int left = 0, right = nums.size() - 1; while(left < right){ int mid = left + (right - left) / 2; if(nums[mid] >= target){ right = mid; }else{ left = mid + 1; } } if(nums[left] != target){ // 返回[-1, -1] return {-1, -1}; } int first = left; // 记录第一个位置 left = 0, right = nums.size() - 1; // 2. 最后一个位置 while(left < right){ int mid = left + (right - left + 1) / 2; if(nums[mid] <= target){ left = mid; }else{ right = mid - 1; } } // 返回结果 return {first, left}; } };
class Solution {
public:
int mySqrt(int x) {
int left = 0, right = 1e9;
while(left < right){
int mid = (right + left + 1) / 2;
if((long long)mid * mid <= x){ // 舍去小数,所以找小于的一边
left = mid;
}else{
right = mid - 1;
}
}
return left;
}
};
class Solution { public: bool isPerfectSquare(int num) { // 暴力:遍历所有int看是否平方 == num;所以使用二分简化 int left = 1, right = 1e9; while(left < right){ int mid = (left + right) >> 1; if((long long)mid * mid >= num){ right = mid; }else{ left = mid + 1; } } if((long long)left * left == num){ return true; } return false; } };
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int fast = 0, slow = 0;
for(; fast < nums.size(); fast++){
if(val != nums[fast]){
nums[slow++] = nums[fast];
}
}
return slow;
}
};
class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.empty()){ return 0; } // 快指针:指向原数组,找到第一个不同的元素 // 慢指针:指向结果数组的位置 // 如果nums[fast] != nums[fast - 1],就把这个不重复元素送到slow位置 int slow = 1, fast = 1; for(; fast < nums.size(); fast++){ if(nums[fast] != nums[fast -1]){ nums[slow++] = nums[fast]; } } return slow; } };
class Solution { public: bool backspaceCompare(string s, string t) { // 1. 先去掉退格字符 int change_s = changeString(s); int change_t = changeString(t); cout << "s = " << s << " t = " << t << endl; cout << change_s << ' ' << change_t << endl; // 2. 比较前几个字符是否相同 if(change_s != change_t){ return false; } for(int i = 0; i < change_s; i++){ if(s[i] != t[i]){ return false; } } return true; } int changeString(string &str){ // 返回前几个字符是有效的 int slow = 0, fast = 0; for(; fast < str.size(); fast++){ if(str[fast] == '#'){ if(slow != 0){ // 如果slow已经在开头,不用动 --slow; } } else{ str[slow++] = str[fast]; } } return slow; } };
class Solution { public: vector<int> sortedSquares(vector<int>& nums) { // 暴力:平方后排序 // for(auto &i : nums){ // i = i * i; // } // sort(nums.begin(), nums.end()); // return nums; // 双指针:两头指针,大的放到后面 int n_size = nums.size() - 1; vector<int> ret(n_size + 1); for(int i = 0, j = n_size, pos = n_size; i <= j; ){ if(nums[i] * nums[i] <= nums[j] * nums[j]){ ret[pos--] = nums[j] * nums[j]; j--; } else{ ret[pos--] = nums[i] * nums[i]; i++; } } return ret; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。