二分法

左闭右闭 [left, right]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int search(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return -1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}

左闭右开 [left, right)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int search(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
l = mid + 1;
} else {
r = mid;
}
}
return -1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return -1;
}

左闭右闭 or 左闭右开,记一种即可

二叉树层次遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (root == NULL) return res;

queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
vector<int> level;
while (size-- > 0) {
TreeNode* node = que.front();
que.pop();
level.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.push_back(level);
}
return res;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;

Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while (!que.isEmpty()) {
int size = que.size();
List<Integer> level = new ArrayList<>();
while (size-- > 0) {
TreeNode node = que.poll();
level.add(node.val);
if (node.left != null) que.offer(node.left);
if (node.right != null) que.offer(node.right);
}
res.add(level);
}
return res;
}

双指针法

移除元素

题目链接:LeetCode27.移除元素

原地移除数组 nums 中所有数值等于 val 的元素,并返回移除后数组的新长度

1
2
3
4
5
6
7
8
9
10
int removeElement(vector<int>& nums, int val) {
int slow = 0, fast = 0;
while (fast < nums.size()) {
if (nums[fast] != val) {
nums[slow++] = nums[fast];
}
++fast;
}
return slow;
}
1
2
3
4
5
6
7
8
9
10
public int removeElement(int[] nums, int val) {
int slow = 0, fast = 0;
while (fast < nums.length) {
if (nums[fast] != val) {
nums[slow++] = nums[fast];
}
++fast;
}
return slow;
}

反转字符串

题目链接:LeetCode344.反转字符串

将输入的字符串(以字符数组 s 的形式给出)原地反转过来

1
2
3
4
5
void reverseString(vector<char>& s) {
for (int l = 0, r = s.size() - 1; l < r; ++l, --r) {
swap(s[l], s[r]);
}
}
1
2
3
4
5
6
7
public void reverseString(char[] s) {
for (int l = 0, r = s.length - 1; l < r; ++l, --r) {
s[l] ^= s[r];
s[r] ^= s[l];
s[l] ^= s[r];
}
}

滑动窗口

题目链接:LeetCode209.长度最小的子数组

找出数组 nums 中满足总和大于等于 target 的长度最小的连续子数组,返回其长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0, right = 0;
int sum = 0;
int minLen = INT32_MAX;
int curLen = 0;
while (right < nums.size()) {
sum += nums[right];
while (sum >= target) {
curLen = right - left + 1;
if ()
if (curLen < minLen) minLen = curLen;
sum -= nums[left++];
}
++right;
}
return minLen == INT32_MAX ? 0 : minLen;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int minSubArrayLen(int target, int[] nums) {
int minLen = Integer.MAX_VALUE;
int sum = 0;
for (int i = 0, j = 0; j < nums.length; ++j) {
sum += nums[j];
while (sum >= target) {
int len = j - i + 1;
if (len == 1) return 1;
minLen = Math.min(minLen, len);
sum -= nums[i++];
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}