359 阅读2分钟

# 二分法

### Leetcode 704. Binary Search

``````class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
int mid;
while(l < r){
mid = l + r >> 1;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}
if(nums[r] != target) return -1;
return r;
}
};
``````

1. mid = l + r + 1 >> 1
那么当check的结果为true时，区间应更新为[mid, r]，更新方式为`l = mid`。 当check的结果为true时，区间应更新为[l, mid - 1]，更新方式为`r = mid - 1`
2. mid = l + r >> 1
那么当check的结果为true时，区间应更新为[l, mid]，更新方式为`r = mid`。 当check的结果为true时，区间应更新为[mid + 1, r]，更新方式为`l = mid + 1`

### Leetcode 33. Search in Rotated Sorted Array

``````class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(nums[mid] >= nums[0]) l = mid;
else r = mid - 1;
}

if(target >= nums[0]) l = 0;
else l = r + 1, r = nums.size() - 1;

while(l < r){
int mid = l + r >> 1;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}

if(nums[r] != target) return -1;
return r;
}
};
``````

• Leetcode 81. Search in Rotated Sorted Array II
``````class Solution {
public:
bool search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;

while(nums[0] == nums[r] && r > 0) r --;
if(r <= 0) return nums[0] == target;

while(l < r){
int mid = l + r + 1 >> 1;
if(nums[mid] >= nums[0]) l = mid;
else r = mid - 1;
}

if(target >= nums[0]) l = 0;
else l = r + 1, r = nums.size() - 1;

while(l < r){
int mid = l + r >> 1;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}

if(nums[r] != target) return false;
return true;
}
};
``````

### Leetcode 69. Sqrt(x)

``````class Solution {
public:
int mySqrt(int x) {
int l = 0, r = x;
while(l < r){
int mid = l + r + 1ll >> 1;
if(mid <= x / mid) l = mid;
else r = mid - 1;
}
return r;
}
};
``````

check函数如果为`mid * mid <= x`会出现数据太大而Runtime Error。

# 双指针

### Leetcode 27. Remove Element

``````class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int k = 0;
for(int i = 0; i < nums.size(); i ++){
if(nums[i] != val) nums[k ++] = nums[i];
}
return k;
}
};
``````

### Leetcode 26. Remove Duplicates from Sorted Array

``````class Solution {
public:
int removeDuplicates(vector<int>& nums) {

int k = 0;
for(int i = 1; i < nums.size();i ++){
if(nums[i] != nums[i - 1]) nums[++ k] = nums[i];
}
return k + 1;
}
};
``````

`nums[i] != nums[i - 1]`时，把`nums[i]`复制到k所在位置，然后k往前走。

### Leetcode 283. Move Zeroes

``````class Solution {
public:
void moveZeroes(vector<int>& nums) {
int k = 0;
for(int i = 0; i < nums.size(); i ++){
if(nums[i] != 0) nums[k ++] = nums[i];
}
for(int j = k; j < nums.size(); j ++) nums[j] = 0;
}
};
``````

`nums[i] != 0`时，把`nums[i]`复制到k所在位置，然后k往前走。

### Leetcode 844. Backspace String Compare

``````class Solution {
public:
bool backspaceCompare(string s, string t) {
return get(s) == get(t);
}
string get(string s){
int slow = 0;
for(int fast = 0; fast < s.size(); fast ++)
{
if(s[fast] != '#')
s[slow ++] = s[fast];
else if(slow != 0)
slow --;
}
s.resize(slow);
return s;
}
};
``````

``````class Solution {
public:
bool backspaceCompare(string s, string t) {
return get(s) == get(t);
}
string get(string s){
string res;
for(auto c : s){
if(c == '#') {
if(res.size()) res.pop_back();
}
else res += c;
}
return res;
}
};
``````

### Leetcode 977. Squares of a Sorted Array

``````class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size();
vector<int> res(n);

for(int i = 0, j = n - 1, k = n - 1; i <= j;){
if(nums[i] * nums[i] >= nums[j] * nums[j]){
res[k --] = nums[i] * nums[i];
i ++;
}
else{
res[k --] = nums[j] * nums[j];
j --;
}
}
return res;
}
};
``````

nums[i]作为数组头，nums[j]作为数组尾，谁的平方大就把谁放入res里，然后向中间移动。