auto commit
This commit is contained in:
parent
316436952b
commit
1ce0d0ec6a
292
docs/notes/Leetcode 题解 - 二分查找.md
Normal file
292
docs/notes/Leetcode 题解 - 二分查找.md
Normal file
|
@ -0,0 +1,292 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [原理](#原理)
|
||||
* [1. 正常实现](#1-正常实现)
|
||||
* [2. 时间复杂度](#2-时间复杂度)
|
||||
* [3. m 计算](#3-m-计算)
|
||||
* [4. 返回值](#4-返回值)
|
||||
* [5. 变种](#5-变种)
|
||||
* [求开方](#求开方)
|
||||
* [大于给定元素的最小元素](#大于给定元素的最小元素)
|
||||
* [有序数组的 Single Element](#有序数组的-single-element)
|
||||
* [第一个错误的版本](#第一个错误的版本)
|
||||
* [旋转数组的最小数字](#旋转数组的最小数字)
|
||||
* [查找区间](#查找区间)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 原理
|
||||
|
||||
## 1. 正常实现
|
||||
|
||||
```java
|
||||
public int binarySearch(int[] nums, int key) {
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l <= h) {
|
||||
int m = l + (h - l) / 2;
|
||||
if (nums[m] == key) {
|
||||
return m;
|
||||
} else if (nums[m] > key) {
|
||||
h = m - 1;
|
||||
} else {
|
||||
l = m + 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
## 2. 时间复杂度
|
||||
|
||||
二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度为 O(logN)。
|
||||
|
||||
## 3. m 计算
|
||||
|
||||
有两种计算中值 m 的方式:
|
||||
|
||||
- m = (l + h) / 2
|
||||
- m = l + (h - l) / 2
|
||||
|
||||
l + h 可能出现加法溢出,最好使用第二种方式。
|
||||
|
||||
## 4. 返回值
|
||||
|
||||
循环退出时如果仍然没有查找到 key,那么表示查找失败。可以有两种返回值:
|
||||
|
||||
- -1:以一个错误码表示没有查找到 key
|
||||
- l:将 key 插入到 nums 中的正确位置
|
||||
|
||||
## 5. 变种
|
||||
|
||||
二分查找可以有很多变种,变种实现要注意边界值的判断。例如在一个有重复元素的数组中查找 key 的最左位置的实现如下:
|
||||
|
||||
```java
|
||||
public int binarySearch(int[] nums, int key) {
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l < h) {
|
||||
int m = l + (h - l) / 2;
|
||||
if (nums[m] >= key) {
|
||||
h = m;
|
||||
} else {
|
||||
l = m + 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
```
|
||||
|
||||
该实现和正常实现有以下不同:
|
||||
|
||||
- 循环条件为 l < h
|
||||
- h 的赋值表达式为 h = m
|
||||
- 最后返回 l 而不是 -1
|
||||
|
||||
在 nums[m] >= key 的情况下,可以推导出最左 key 位于 [l, m] 区间中,这是一个闭区间。h 的赋值表达式为 h = m,因为 m 位置也可能是解。
|
||||
|
||||
在 h 的赋值表达式为 h = mid 的情况下,如果循环条件为 l <= h,那么会出现循环无法退出的情况,因此循环条件只能是 l < h。以下演示了循环条件为 l <= h 时循环无法退出的情况:
|
||||
|
||||
```text
|
||||
nums = {0, 1, 2}, key = 1
|
||||
l m h
|
||||
0 1 2 nums[m] >= key
|
||||
0 0 1 nums[m] < key
|
||||
1 1 1 nums[m] >= key
|
||||
1 1 1 nums[m] >= key
|
||||
...
|
||||
```
|
||||
|
||||
当循环体退出时,不表示没有查找到 key,因此最后返回的结果不应该为 -1。为了验证有没有查找到,需要在调用端判断一下返回位置上的值和 key 是否相等。
|
||||
|
||||
# 求开方
|
||||
|
||||
[69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/)
|
||||
|
||||
```html
|
||||
Input: 4
|
||||
Output: 2
|
||||
|
||||
Input: 8
|
||||
Output: 2
|
||||
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.
|
||||
```
|
||||
|
||||
一个数 x 的开方 sqrt 一定在 0 \~ x 之间,并且满足 sqrt == x / sqrt。可以利用二分查找在 0 \~ x 之间查找 sqrt。
|
||||
|
||||
对于 x = 8,它的开方是 2.82842...,最后应该返回 2 而不是 3。在循环条件为 l <= h 并且循环退出时,h 总是比 l 小 1,也就是说 h = 2,l = 3,因此最后的返回值应该为 h 而不是 l。
|
||||
|
||||
```java
|
||||
public int mySqrt(int x) {
|
||||
if (x <= 1) {
|
||||
return x;
|
||||
}
|
||||
int l = 1, h = x;
|
||||
while (l <= h) {
|
||||
int mid = l + (h - l) / 2;
|
||||
int sqrt = x / mid;
|
||||
if (sqrt == mid) {
|
||||
return mid;
|
||||
} else if (mid > sqrt) {
|
||||
h = mid - 1;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
return h;
|
||||
}
|
||||
```
|
||||
|
||||
# 大于给定元素的最小元素
|
||||
|
||||
[744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
letters = ["c", "f", "j"]
|
||||
target = "d"
|
||||
Output: "f"
|
||||
|
||||
Input:
|
||||
letters = ["c", "f", "j"]
|
||||
target = "k"
|
||||
Output: "c"
|
||||
```
|
||||
|
||||
题目描述:给定一个有序的字符数组 letters 和一个字符 target,要求找出 letters 中大于 target 的最小字符,如果找不到就返回第 1 个字符。
|
||||
|
||||
```java
|
||||
public char nextGreatestLetter(char[] letters, char target) {
|
||||
int n = letters.length;
|
||||
int l = 0, h = n - 1;
|
||||
while (l <= h) {
|
||||
int m = l + (h - l) / 2;
|
||||
if (letters[m] <= target) {
|
||||
l = m + 1;
|
||||
} else {
|
||||
h = m - 1;
|
||||
}
|
||||
}
|
||||
return l < n ? letters[l] : letters[0];
|
||||
}
|
||||
```
|
||||
|
||||
# 有序数组的 Single Element
|
||||
|
||||
[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
|
||||
|
||||
```html
|
||||
Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
|
||||
Output: 2
|
||||
```
|
||||
|
||||
题目描述:一个有序数组只有一个数不出现两次,找出这个数。要求以 O(logN) 时间复杂度进行求解。
|
||||
|
||||
令 index 为 Single Element 在数组中的位置。如果 m 为偶数,并且 m + 1 < index,那么 nums[m] == nums[m + 1];m + 1 >= index,那么 nums[m] != nums[m + 1]。
|
||||
|
||||
从上面的规律可以知道,如果 nums[m] == nums[m + 1],那么 index 所在的数组位置为 [m + 2, h],此时令 l = m + 2;如果 nums[m] != nums[m + 1],那么 index 所在的数组位置为 [l, m],此时令 h = m。
|
||||
|
||||
因为 h 的赋值表达式为 h = m,那么循环条件也就只能使用 l < h 这种形式。
|
||||
|
||||
```java
|
||||
public int singleNonDuplicate(int[] nums) {
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l < h) {
|
||||
int m = l + (h - l) / 2;
|
||||
if (m % 2 == 1) {
|
||||
m--; // 保证 l/h/m 都在偶数位,使得查找区间大小一直都是奇数
|
||||
}
|
||||
if (nums[m] == nums[m + 1]) {
|
||||
l = m + 2;
|
||||
} else {
|
||||
h = m;
|
||||
}
|
||||
}
|
||||
return nums[l];
|
||||
}
|
||||
```
|
||||
|
||||
# 第一个错误的版本
|
||||
|
||||
[278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/)
|
||||
|
||||
题目描述:给定一个元素 n 代表有 [1, 2, ..., n] 版本,可以调用 isBadVersion(int x) 知道某个版本是否错误,要求找到第一个错误的版本。
|
||||
|
||||
如果第 m 个版本出错,则表示第一个错误的版本在 [l, m] 之间,令 h = m;否则第一个错误的版本在 [m + 1, h] 之间,令 l = m + 1。
|
||||
|
||||
因为 h 的赋值表达式为 h = m,因此循环条件为 l < h。
|
||||
|
||||
```java
|
||||
public int firstBadVersion(int n) {
|
||||
int l = 1, h = n;
|
||||
while (l < h) {
|
||||
int mid = l + (h - l) / 2;
|
||||
if (isBadVersion(mid)) {
|
||||
h = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
```
|
||||
|
||||
# 旋转数组的最小数字
|
||||
|
||||
[153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
|
||||
|
||||
```html
|
||||
Input: [3,4,5,1,2],
|
||||
Output: 1
|
||||
```
|
||||
|
||||
```java
|
||||
public int findMin(int[] nums) {
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l < h) {
|
||||
int m = l + (h - l) / 2;
|
||||
if (nums[m] <= nums[h]) {
|
||||
h = m;
|
||||
} else {
|
||||
l = m + 1;
|
||||
}
|
||||
}
|
||||
return nums[l];
|
||||
}
|
||||
```
|
||||
|
||||
# 查找区间
|
||||
|
||||
[34. Search for a Range (Medium)](https://leetcode.com/problems/search-for-a-range/description/)
|
||||
|
||||
```html
|
||||
Input: nums = [5,7,7,8,8,10], target = 8
|
||||
Output: [3,4]
|
||||
|
||||
Input: nums = [5,7,7,8,8,10], target = 6
|
||||
Output: [-1,-1]
|
||||
```
|
||||
|
||||
```java
|
||||
public int[] searchRange(int[] nums, int target) {
|
||||
int first = binarySearch(nums, target);
|
||||
int last = binarySearch(nums, target + 1) - 1;
|
||||
if (first == nums.length || nums[first] != target) {
|
||||
return new int[]{-1, -1};
|
||||
} else {
|
||||
return new int[]{first, Math.max(first, last)};
|
||||
}
|
||||
}
|
||||
|
||||
private int binarySearch(int[] nums, int target) {
|
||||
int l = 0, h = nums.length; // 注意 h 的初始值
|
||||
while (l < h) {
|
||||
int m = l + (h - l) / 2;
|
||||
if (nums[m] >= target) {
|
||||
h = m;
|
||||
} else {
|
||||
l = m + 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
```
|
||||
|
428
docs/notes/Leetcode 题解 - 位运算.md
Normal file
428
docs/notes/Leetcode 题解 - 位运算.md
Normal file
|
@ -0,0 +1,428 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [原理](#原理)
|
||||
* [1. 基本原理](#1-基本原理)
|
||||
* [2. mask 计算](#2-mask-计算)
|
||||
* [3. Java 中的位操作](#3-java-中的位操作)
|
||||
* [例题](#例题)
|
||||
* [统计两个数的二进制表示有多少位不同](#统计两个数的二进制表示有多少位不同)
|
||||
* [数组中唯一一个不重复的元素](#数组中唯一一个不重复的元素)
|
||||
* [找出数组中缺失的那个数](#找出数组中缺失的那个数)
|
||||
* [数组中不重复的两个元素](#数组中不重复的两个元素)
|
||||
* [翻转一个数的比特位](#翻转一个数的比特位)
|
||||
* [不用额外变量交换两个整数](#不用额外变量交换两个整数)
|
||||
* [判断一个数是不是 2 的 n 次方](#判断一个数是不是-2-的-n-次方)
|
||||
* [判断一个数是不是 4 的 n 次方](#判断一个数是不是-4-的-n-次方)
|
||||
* [判断一个数的位级表示是否不会出现连续的 0 和 1](#判断一个数的位级表示是否不会出现连续的-0-和-1)
|
||||
* [求一个数的补码](#求一个数的补码)
|
||||
* [实现整数的加法](#实现整数的加法)
|
||||
* [字符串数组最大乘积](#字符串数组最大乘积)
|
||||
* [统计从 0 \~ n 每个数的二进制表示中 1 的个数](#统计从-0-\~-n-每个数的二进制表示中-1-的个数)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 原理
|
||||
|
||||
## 1. 基本原理
|
||||
|
||||
0s 表示一串 0,1s 表示一串 1。
|
||||
|
||||
```
|
||||
x ^ 0s = x x & 0s = 0 x | 0s = x
|
||||
x ^ 1s = ~x x & 1s = x x | 1s = 1s
|
||||
x ^ x = 0 x & x = x x | x = x
|
||||
```
|
||||
|
||||
- 利用 x ^ 1s = \~x 的特点,可以将位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另一个数。
|
||||
- 利用 x & 0s = 0 和 x & 1s = x 的特点,可以实现掩码操作。一个数 num 与 mask:00111100 进行位与操作,只保留 num 中与 mask 的 1 部分相对应的位。
|
||||
- 利用 x | 0s = x 和 x | 1s = 1s 的特点,可以实现设值操作。一个数 num 与 mask:00111100 进行位或操作,将 num 中与 mask 的 1 部分相对应的位都设置为 1。
|
||||
|
||||
位与运算技巧:
|
||||
|
||||
- n&(n-1) 去除 n 的位级表示中最低的那一位。例如对于二进制表示 10110100,减去 1 得到 10110011,这两个数相与得到 10110000。
|
||||
- n&(-n) 得到 n 的位级表示中最低的那一位。-n 得到 n 的反码加 1,对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100。
|
||||
- n-n&(\~n+1) 去除 n 的位级表示中最高的那一位。
|
||||
|
||||
移位运算:
|
||||
|
||||
- \>\> n 为算术右移,相当于除以 2<sup>n</sup>;
|
||||
- \>\>\> n 为无符号右移,左边会补上 0。
|
||||
- << n 为算术左移,相当于乘以 2<sup>n</sup>。
|
||||
|
||||
## 2. mask 计算
|
||||
|
||||
要获取 111111111,将 0 取反即可,\~0。
|
||||
|
||||
要得到只有第 i 位为 1 的 mask,将 1 向左移动 i-1 位即可,1<<(i-1) 。例如 1<<4 得到只有第 5 位为 1 的 mask :00010000。
|
||||
|
||||
要得到 1 到 i 位为 1 的 mask,1<<(i+1)-1 即可,例如将 1<<(4+1)-1 = 00010000-1 = 00001111。
|
||||
|
||||
要得到 1 到 i 位为 0 的 mask,只需将 1 到 i 位为 1 的 mask 取反,即 \~(1<<(i+1)-1)。
|
||||
|
||||
## 3. Java 中的位操作
|
||||
|
||||
```html
|
||||
static int Integer.bitCount(); // 统计 1 的数量
|
||||
static int Integer.highestOneBit(); // 获得最高位
|
||||
static String toBinaryString(int i); // 转换为二进制表示的字符串
|
||||
```
|
||||
|
||||
# 例题
|
||||
|
||||
## 统计两个数的二进制表示有多少位不同
|
||||
|
||||
[461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/)
|
||||
|
||||
```html
|
||||
Input: x = 1, y = 4
|
||||
|
||||
Output: 2
|
||||
|
||||
Explanation:
|
||||
1 (0 0 0 1)
|
||||
4 (0 1 0 0)
|
||||
↑ ↑
|
||||
|
||||
The above arrows point to positions where the corresponding bits are different.
|
||||
```
|
||||
|
||||
对两个数进行异或操作,位级表示不同的那一位为 1,统计有多少个 1 即可。
|
||||
|
||||
```java
|
||||
public int hammingDistance(int x, int y) {
|
||||
int z = x ^ y;
|
||||
int cnt = 0;
|
||||
while(z != 0) {
|
||||
if ((z & 1) == 1) cnt++;
|
||||
z = z >> 1;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
```
|
||||
|
||||
使用 z&(z-1) 去除 z 位级表示最低的那一位。
|
||||
|
||||
```java
|
||||
public int hammingDistance(int x, int y) {
|
||||
int z = x ^ y;
|
||||
int cnt = 0;
|
||||
while (z != 0) {
|
||||
z &= (z - 1);
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
```
|
||||
|
||||
可以使用 Integer.bitcount() 来统计 1 个的个数。
|
||||
|
||||
```java
|
||||
public int hammingDistance(int x, int y) {
|
||||
return Integer.bitCount(x ^ y);
|
||||
}
|
||||
```
|
||||
|
||||
## 数组中唯一一个不重复的元素
|
||||
|
||||
[136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/)
|
||||
|
||||
```html
|
||||
Input: [4,1,2,1,2]
|
||||
Output: 4
|
||||
```
|
||||
|
||||
两个相同的数异或的结果为 0,对所有数进行异或操作,最后的结果就是单独出现的那个数。
|
||||
|
||||
```java
|
||||
public int singleNumber(int[] nums) {
|
||||
int ret = 0;
|
||||
for (int n : nums) ret = ret ^ n;
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
## 找出数组中缺失的那个数
|
||||
|
||||
[268. Missing Number (Easy)](https://leetcode.com/problems/missing-number/description/)
|
||||
|
||||
```html
|
||||
Input: [3,0,1]
|
||||
Output: 2
|
||||
```
|
||||
|
||||
题目描述:数组元素在 0-n 之间,但是有一个数是缺失的,要求找到这个缺失的数。
|
||||
|
||||
```java
|
||||
public int missingNumber(int[] nums) {
|
||||
int ret = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
ret = ret ^ i ^ nums[i];
|
||||
}
|
||||
return ret ^ nums.length;
|
||||
}
|
||||
```
|
||||
|
||||
## 数组中不重复的两个元素
|
||||
|
||||
[260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/)
|
||||
|
||||
两个不相等的元素在位级表示上必定会有一位存在不同。
|
||||
|
||||
将数组的所有元素异或得到的结果为不存在重复的两个元素异或的结果。
|
||||
|
||||
diff &= -diff 得到出 diff 最右侧不为 0 的位,也就是不存在重复的两个元素在位级表示上最右侧不同的那一位,利用这一位就可以将两个元素区分开来。
|
||||
|
||||
```java
|
||||
public int[] singleNumber(int[] nums) {
|
||||
int diff = 0;
|
||||
for (int num : nums) diff ^= num;
|
||||
diff &= -diff; // 得到最右一位
|
||||
int[] ret = new int[2];
|
||||
for (int num : nums) {
|
||||
if ((num & diff) == 0) ret[0] ^= num;
|
||||
else ret[1] ^= num;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
## 翻转一个数的比特位
|
||||
|
||||
[190. Reverse Bits (Easy)](https://leetcode.com/problems/reverse-bits/description/)
|
||||
|
||||
```java
|
||||
public int reverseBits(int n) {
|
||||
int ret = 0;
|
||||
for (int i = 0; i < 32; i++) {
|
||||
ret <<= 1;
|
||||
ret |= (n & 1);
|
||||
n >>>= 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
如果该函数需要被调用很多次,可以将 int 拆成 4 个 byte,然后缓存 byte 对应的比特位翻转,最后再拼接起来。
|
||||
|
||||
```java
|
||||
private static Map<Byte, Integer> cache = new HashMap<>();
|
||||
|
||||
public int reverseBits(int n) {
|
||||
int ret = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
ret <<= 8;
|
||||
ret |= reverseByte((byte) (n & 0b11111111));
|
||||
n >>= 8;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int reverseByte(byte b) {
|
||||
if (cache.containsKey(b)) return cache.get(b);
|
||||
int ret = 0;
|
||||
byte t = b;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ret <<= 1;
|
||||
ret |= t & 1;
|
||||
t >>= 1;
|
||||
}
|
||||
cache.put(b, ret);
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
## 不用额外变量交换两个整数
|
||||
|
||||
[程序员代码面试指南 :P317](#)
|
||||
|
||||
```java
|
||||
a = a ^ b;
|
||||
b = a ^ b;
|
||||
a = a ^ b;
|
||||
```
|
||||
|
||||
## 判断一个数是不是 2 的 n 次方
|
||||
|
||||
[231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/)
|
||||
|
||||
二进制表示只有一个 1 存在。
|
||||
|
||||
```java
|
||||
public boolean isPowerOfTwo(int n) {
|
||||
return n > 0 && Integer.bitCount(n) == 1;
|
||||
}
|
||||
```
|
||||
|
||||
利用 1000 & 0111 == 0 这种性质,得到以下解法:
|
||||
|
||||
```java
|
||||
public boolean isPowerOfTwo(int n) {
|
||||
return n > 0 && (n & (n - 1)) == 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 判断一个数是不是 4 的 n 次方
|
||||
|
||||
[342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/)
|
||||
|
||||
这种数在二进制表示中有且只有一个奇数位为 1,例如 16(10000)。
|
||||
|
||||
```java
|
||||
public boolean isPowerOfFour(int num) {
|
||||
return num > 0 && (num & (num - 1)) == 0 && (num & 0b01010101010101010101010101010101) != 0;
|
||||
}
|
||||
```
|
||||
|
||||
也可以使用正则表达式进行匹配。
|
||||
|
||||
```java
|
||||
public boolean isPowerOfFour(int num) {
|
||||
return Integer.toString(num, 4).matches("10*");
|
||||
}
|
||||
```
|
||||
|
||||
## 判断一个数的位级表示是否不会出现连续的 0 和 1
|
||||
|
||||
[693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/)
|
||||
|
||||
```html
|
||||
Input: 10
|
||||
Output: True
|
||||
Explanation:
|
||||
The binary representation of 10 is: 1010.
|
||||
|
||||
Input: 11
|
||||
Output: False
|
||||
Explanation:
|
||||
The binary representation of 11 is: 1011.
|
||||
```
|
||||
|
||||
对于 1010 这种位级表示的数,把它向右移动 1 位得到 101,这两个数每个位都不同,因此异或得到的结果为 1111。
|
||||
|
||||
```java
|
||||
public boolean hasAlternatingBits(int n) {
|
||||
int a = (n ^ (n >> 1));
|
||||
return (a & (a + 1)) == 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 求一个数的补码
|
||||
|
||||
[476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/)
|
||||
|
||||
```html
|
||||
Input: 5
|
||||
Output: 2
|
||||
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
|
||||
```
|
||||
|
||||
题目描述:不考虑二进制表示中的首 0 部分。
|
||||
|
||||
对于 00000101,要求补码可以将它与 00000111 进行异或操作。那么问题就转换为求掩码 00000111。
|
||||
|
||||
```java
|
||||
public int findComplement(int num) {
|
||||
if (num == 0) return 1;
|
||||
int mask = 1 << 30;
|
||||
while ((num & mask) == 0) mask >>= 1;
|
||||
mask = (mask << 1) - 1;
|
||||
return num ^ mask;
|
||||
}
|
||||
```
|
||||
|
||||
可以利用 Java 的 Integer.highestOneBit() 方法来获得含有首 1 的数。
|
||||
|
||||
```java
|
||||
public int findComplement(int num) {
|
||||
if (num == 0) return 1;
|
||||
int mask = Integer.highestOneBit(num);
|
||||
mask = (mask << 1) - 1;
|
||||
return num ^ mask;
|
||||
}
|
||||
```
|
||||
|
||||
对于 10000000 这样的数要扩展成 11111111,可以利用以下方法:
|
||||
|
||||
```html
|
||||
mask |= mask >> 1 11000000
|
||||
mask |= mask >> 2 11110000
|
||||
mask |= mask >> 4 11111111
|
||||
```
|
||||
|
||||
```java
|
||||
public int findComplement(int num) {
|
||||
int mask = num;
|
||||
mask |= mask >> 1;
|
||||
mask |= mask >> 2;
|
||||
mask |= mask >> 4;
|
||||
mask |= mask >> 8;
|
||||
mask |= mask >> 16;
|
||||
return (mask ^ num);
|
||||
}
|
||||
```
|
||||
|
||||
## 实现整数的加法
|
||||
|
||||
[371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/)
|
||||
|
||||
a ^ b 表示没有考虑进位的情况下两数的和,(a & b) << 1 就是进位。
|
||||
|
||||
递归会终止的原因是 (a & b) << 1 最右边会多一个 0,那么继续递归,进位最右边的 0 会慢慢增多,最后进位会变为 0,递归终止。
|
||||
|
||||
```java
|
||||
public int getSum(int a, int b) {
|
||||
return b == 0 ? a : getSum((a ^ b), (a & b) << 1);
|
||||
}
|
||||
```
|
||||
|
||||
## 字符串数组最大乘积
|
||||
|
||||
[318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
|
||||
|
||||
```html
|
||||
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
|
||||
Return 16
|
||||
The two words can be "abcw", "xtfn".
|
||||
```
|
||||
|
||||
题目描述:字符串数组的字符串只含有小写字符。求解字符串数组中两个字符串长度的最大乘积,要求这两个字符串不能含有相同字符。
|
||||
|
||||
本题主要问题是判断两个字符串是否含相同字符,由于字符串只含有小写字符,总共 26 位,因此可以用一个 32 位的整数来存储每个字符是否出现过。
|
||||
|
||||
```java
|
||||
public int maxProduct(String[] words) {
|
||||
int n = words.length;
|
||||
int[] val = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (char c : words[i].toCharArray()) {
|
||||
val[i] |= 1 << (c - 'a');
|
||||
}
|
||||
}
|
||||
int ret = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if ((val[i] & val[j]) == 0) {
|
||||
ret = Math.max(ret, words[i].length() * words[j].length());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
## 统计从 0 \~ n 每个数的二进制表示中 1 的个数
|
||||
|
||||
[338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/)
|
||||
|
||||
对于数字 6(110),它可以看成是 4(100) 再加一个 2(10),因此 dp[i] = dp[i&(i-1)] + 1;
|
||||
|
||||
```java
|
||||
public int[] countBits(int num) {
|
||||
int[] ret = new int[num + 1];
|
||||
for(int i = 1; i <= num; i++){
|
||||
ret[i] = ret[i&(i-1)] + 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
50
docs/notes/Leetcode 题解 - 分治.md
Normal file
50
docs/notes/Leetcode 题解 - 分治.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [给表达式加括号](#给表达式加括号)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 给表达式加括号
|
||||
|
||||
[241. Different Ways to Add Parentheses (Medium)](https://leetcode.com/problems/different-ways-to-add-parentheses/description/)
|
||||
|
||||
```html
|
||||
Input: "2-1-1".
|
||||
|
||||
((2-1)-1) = 0
|
||||
(2-(1-1)) = 2
|
||||
|
||||
Output : [0, 2]
|
||||
```
|
||||
|
||||
```java
|
||||
public List<Integer> diffWaysToCompute(String input) {
|
||||
List<Integer> ways = new ArrayList<>();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c == '+' || c == '-' || c == '*') {
|
||||
List<Integer> left = diffWaysToCompute(input.substring(0, i));
|
||||
List<Integer> right = diffWaysToCompute(input.substring(i + 1));
|
||||
for (int l : left) {
|
||||
for (int r : right) {
|
||||
switch (c) {
|
||||
case '+':
|
||||
ways.add(l + r);
|
||||
break;
|
||||
case '-':
|
||||
ways.add(l - r);
|
||||
break;
|
||||
case '*':
|
||||
ways.add(l * r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ways.size() == 0) {
|
||||
ways.add(Integer.valueOf(input));
|
||||
}
|
||||
return ways;
|
||||
}
|
||||
```
|
1222
docs/notes/Leetcode 题解 - 动态规划.md
Normal file
1222
docs/notes/Leetcode 题解 - 动态规划.md
Normal file
File diff suppressed because it is too large
Load Diff
239
docs/notes/Leetcode 题解 - 双指针.md
Normal file
239
docs/notes/Leetcode 题解 - 双指针.md
Normal file
|
@ -0,0 +1,239 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [有序数组的 Two Sum](#有序数组的-two-sum)
|
||||
* [两数平方和](#两数平方和)
|
||||
* [反转字符串中的元音字符](#反转字符串中的元音字符)
|
||||
* [回文字符串](#回文字符串)
|
||||
* [归并两个有序数组](#归并两个有序数组)
|
||||
* [判断链表是否存在环](#判断链表是否存在环)
|
||||
* [最长子序列](#最长子序列)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。
|
||||
|
||||
# 有序数组的 Two Sum
|
||||
|
||||
[Leetcode :167. Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)
|
||||
|
||||
```html
|
||||
Input: numbers={2, 7, 11, 15}, target=9
|
||||
Output: index1=1, index2=2
|
||||
```
|
||||
|
||||
题目描述:在有序数组中找出两个数,使它们的和为 target。
|
||||
|
||||
使用双指针,一个指针指向值较小的元素,一个指针指向值较大的元素。指向较小元素的指针从头向尾遍历,指向较大元素的指针从尾向头遍历。
|
||||
|
||||
- 如果两个指针指向元素的和 sum == target,那么得到要求的结果;
|
||||
- 如果 sum > target,移动较大的元素,使 sum 变小一些;
|
||||
- 如果 sum < target,移动较小的元素,使 sum 变大一些。
|
||||
|
||||
```java
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
int i = 0, j = numbers.length - 1;
|
||||
while (i < j) {
|
||||
int sum = numbers[i] + numbers[j];
|
||||
if (sum == target) {
|
||||
return new int[]{i + 1, j + 1};
|
||||
} else if (sum < target) {
|
||||
i++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
# 两数平方和
|
||||
|
||||
[633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/)
|
||||
|
||||
```html
|
||||
Input: 5
|
||||
Output: True
|
||||
Explanation: 1 * 1 + 2 * 2 = 5
|
||||
```
|
||||
|
||||
题目描述:判断一个数是否为两个数的平方和。
|
||||
|
||||
```java
|
||||
public boolean judgeSquareSum(int c) {
|
||||
int i = 0, j = (int) Math.sqrt(c);
|
||||
while (i <= j) {
|
||||
int powSum = i * i + j * j;
|
||||
if (powSum == c) {
|
||||
return true;
|
||||
} else if (powSum > c) {
|
||||
j--;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
# 反转字符串中的元音字符
|
||||
|
||||
[345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)
|
||||
|
||||
```html
|
||||
Given s = "leetcode", return "leotcede".
|
||||
```
|
||||
|
||||
使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。
|
||||
|
||||
```java
|
||||
private final static HashSet<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
|
||||
|
||||
public String reverseVowels(String s) {
|
||||
int i = 0, j = s.length() - 1;
|
||||
char[] result = new char[s.length()];
|
||||
while (i <= j) {
|
||||
char ci = s.charAt(i);
|
||||
char cj = s.charAt(j);
|
||||
if (!vowels.contains(ci)) {
|
||||
result[i++] = ci;
|
||||
} else if (!vowels.contains(cj)) {
|
||||
result[j--] = cj;
|
||||
} else {
|
||||
result[i++] = cj;
|
||||
result[j--] = ci;
|
||||
}
|
||||
}
|
||||
return new String(result);
|
||||
}
|
||||
```
|
||||
|
||||
# 回文字符串
|
||||
|
||||
[680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/)
|
||||
|
||||
```html
|
||||
Input: "abca"
|
||||
Output: True
|
||||
Explanation: You could delete the character 'c'.
|
||||
```
|
||||
|
||||
题目描述:可以删除一个字符,判断是否能构成回文字符串。
|
||||
|
||||
```java
|
||||
public boolean validPalindrome(String s) {
|
||||
int i = -1, j = s.length();
|
||||
while (++i < --j) {
|
||||
if (s.charAt(i) != s.charAt(j)) {
|
||||
return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isPalindrome(String s, int i, int j) {
|
||||
while (i < j) {
|
||||
if (s.charAt(i++) != s.charAt(j--)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 归并两个有序数组
|
||||
|
||||
[88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
nums1 = [1,2,3,0,0,0], m = 3
|
||||
nums2 = [2,5,6], n = 3
|
||||
|
||||
Output: [1,2,2,3,5,6]
|
||||
```
|
||||
|
||||
题目描述:把归并结果存到第一个数组上。
|
||||
|
||||
需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值。
|
||||
|
||||
```java
|
||||
public void merge(int[] nums1, int m, int[] nums2, int n) {
|
||||
int index1 = m - 1, index2 = n - 1;
|
||||
int indexMerge = m + n - 1;
|
||||
while (index1 >= 0 || index2 >= 0) {
|
||||
if (index1 < 0) {
|
||||
nums1[indexMerge--] = nums2[index2--];
|
||||
} else if (index2 < 0) {
|
||||
nums1[indexMerge--] = nums1[index1--];
|
||||
} else if (nums1[index1] > nums2[index2]) {
|
||||
nums1[indexMerge--] = nums1[index1--];
|
||||
} else {
|
||||
nums1[indexMerge--] = nums2[index2--];
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 判断链表是否存在环
|
||||
|
||||
[141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/)
|
||||
|
||||
使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇。
|
||||
|
||||
```java
|
||||
public boolean hasCycle(ListNode head) {
|
||||
if (head == null) {
|
||||
return false;
|
||||
}
|
||||
ListNode l1 = head, l2 = head.next;
|
||||
while (l1 != null && l2 != null && l2.next != null) {
|
||||
if (l1 == l2) {
|
||||
return true;
|
||||
}
|
||||
l1 = l1.next;
|
||||
l2 = l2.next.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
# 最长子序列
|
||||
|
||||
[524. Longest Word in Dictionary through Deleting (Medium)](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/)
|
||||
|
||||
```
|
||||
Input:
|
||||
s = "abpcplea", d = ["ale","apple","monkey","plea"]
|
||||
|
||||
Output:
|
||||
"apple"
|
||||
```
|
||||
|
||||
题目描述:删除 s 中的一些字符,使得它构成字符串列表 d 中的一个字符串,找出能构成的最长字符串。如果有多个相同长度的结果,返回字典序的最小字符串。
|
||||
|
||||
```java
|
||||
public String findLongestWord(String s, List<String> d) {
|
||||
String longestWord = "";
|
||||
for (String target : d) {
|
||||
int l1 = longestWord.length(), l2 = target.length();
|
||||
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
|
||||
continue;
|
||||
}
|
||||
if (isValid(s, target)) {
|
||||
longestWord = target;
|
||||
}
|
||||
}
|
||||
return longestWord;
|
||||
}
|
||||
|
||||
private boolean isValid(String s, String target) {
|
||||
int i = 0, j = 0;
|
||||
while (i < s.length() && j < target.length()) {
|
||||
if (s.charAt(i) == target.charAt(j)) {
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return j == target.length();
|
||||
}
|
||||
```
|
124
docs/notes/Leetcode 题解 - 哈希表.md
Normal file
124
docs/notes/Leetcode 题解 - 哈希表.md
Normal file
|
@ -0,0 +1,124 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [数组中两个数的和为给定值](#数组中两个数的和为给定值)
|
||||
* [判断数组是否含有重复元素](#判断数组是否含有重复元素)
|
||||
* [最长和谐序列](#最长和谐序列)
|
||||
* [最长连续序列](#最长连续序列)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
哈希表使用 O(N) 空间复杂度存储数据,并且以 O(1) 时间复杂度求解问题。
|
||||
|
||||
- Java 中的 **HashSet** 用于存储一个集合,可以查找元素是否在集合中。如果元素有穷,并且范围不大,那么可以用一个布尔数组来存储一个元素是否存在。例如对于只有小写字符的元素,就可以用一个长度为 26 的布尔数组来存储一个字符集合,使得空间复杂度降低为 O(1)。
|
||||
|
||||
- Java 中的 **HashMap** 主要用于映射关系,从而把两个元素联系起来。HashMap 也可以用来对元素进行计数统计,此时键为元素,值为计数。和 HashSet 类似,如果元素有穷并且范围不大,可以用整型数组来进行统计。在对一个内容进行压缩或者其它转换时,利用 HashMap 可以把原始内容和转换后的内容联系起来。例如在一个简化 url 的系统中 [Leetcdoe : 535. Encode and Decode TinyURL (Medium)](https://leetcode.com/problems/encode-and-decode-tinyurl/description/),利用 HashMap 就可以存储精简后的 url 到原始 url 的映射,使得不仅可以显示简化的 url,也可以根据简化的 url 得到原始 url 从而定位到正确的资源。
|
||||
|
||||
|
||||
# 数组中两个数的和为给定值
|
||||
|
||||
[1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/)
|
||||
|
||||
可以先对数组进行排序,然后使用双指针方法或者二分查找方法。这样做的时间复杂度为 O(NlogN),空间复杂度为 O(1)。
|
||||
|
||||
用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i],如果存在说明 target - nums[i] 所在的索引和 i 就是要找的两个数。该方法的时间复杂度为 O(N),空间复杂度为 O(N),使用空间来换取时间。
|
||||
|
||||
```java
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
HashMap<Integer, Integer> indexForNum = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (indexForNum.containsKey(target - nums[i])) {
|
||||
return new int[]{indexForNum.get(target - nums[i]), i};
|
||||
} else {
|
||||
indexForNum.put(nums[i], i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
# 判断数组是否含有重复元素
|
||||
|
||||
[217. Contains Duplicate (Easy)](https://leetcode.com/problems/contains-duplicate/description/)
|
||||
|
||||
```java
|
||||
public boolean containsDuplicate(int[] nums) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
for (int num : nums) {
|
||||
set.add(num);
|
||||
}
|
||||
return set.size() < nums.length;
|
||||
}
|
||||
```
|
||||
|
||||
# 最长和谐序列
|
||||
|
||||
[594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/)
|
||||
|
||||
```html
|
||||
Input: [1,3,2,2,5,2,3,7]
|
||||
Output: 5
|
||||
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
|
||||
```
|
||||
|
||||
和谐序列中最大数和最小数之差正好为 1,应该注意的是序列的元素不一定是数组的连续元素。
|
||||
|
||||
```java
|
||||
public int findLHS(int[] nums) {
|
||||
Map<Integer, Integer> countForNum = new HashMap<>();
|
||||
for (int num : nums) {
|
||||
countForNum.put(num, countForNum.getOrDefault(num, 0) + 1);
|
||||
}
|
||||
int longest = 0;
|
||||
for (int num : countForNum.keySet()) {
|
||||
if (countForNum.containsKey(num + 1)) {
|
||||
longest = Math.max(longest, countForNum.get(num + 1) + countForNum.get(num));
|
||||
}
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
```
|
||||
|
||||
# 最长连续序列
|
||||
|
||||
[128. Longest Consecutive Sequence (Hard)](https://leetcode.com/problems/longest-consecutive-sequence/description/)
|
||||
|
||||
```html
|
||||
Given [100, 4, 200, 1, 3, 2],
|
||||
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
|
||||
```
|
||||
|
||||
要求以 O(N) 的时间复杂度求解。
|
||||
|
||||
```java
|
||||
public int longestConsecutive(int[] nums) {
|
||||
Map<Integer, Integer> countForNum = new HashMap<>();
|
||||
for (int num : nums) {
|
||||
countForNum.put(num, 1);
|
||||
}
|
||||
for (int num : nums) {
|
||||
forward(countForNum, num);
|
||||
}
|
||||
return maxCount(countForNum);
|
||||
}
|
||||
|
||||
private int forward(Map<Integer, Integer> countForNum, int num) {
|
||||
if (!countForNum.containsKey(num)) {
|
||||
return 0;
|
||||
}
|
||||
int cnt = countForNum.get(num);
|
||||
if (cnt > 1) {
|
||||
return cnt;
|
||||
}
|
||||
cnt = forward(countForNum, num + 1) + 1;
|
||||
countForNum.put(num, cnt);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
private int maxCount(Map<Integer, Integer> countForNum) {
|
||||
int max = 0;
|
||||
for (int num : countForNum.keySet()) {
|
||||
max = Math.max(max, countForNum.get(num));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
258
docs/notes/Leetcode 题解 - 图.md
Normal file
258
docs/notes/Leetcode 题解 - 图.md
Normal file
|
@ -0,0 +1,258 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [二分图](#二分图)
|
||||
* [判断是否为二分图](#判断是否为二分图)
|
||||
* [拓扑排序](#拓扑排序)
|
||||
* [课程安排的合法性](#课程安排的合法性)
|
||||
* [课程安排的顺序](#课程安排的顺序)
|
||||
* [并查集](#并查集)
|
||||
* [冗余连接](#冗余连接)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 二分图
|
||||
|
||||
如果可以用两种颜色对图中的节点进行着色,并且保证相邻的节点颜色不同,那么这个图就是二分图。
|
||||
|
||||
## 判断是否为二分图
|
||||
|
||||
[785. Is Graph Bipartite? (Medium)](https://leetcode.com/problems/is-graph-bipartite/description/)
|
||||
|
||||
```html
|
||||
Input: [[1,3], [0,2], [1,3], [0,2]]
|
||||
Output: true
|
||||
Explanation:
|
||||
The graph looks like this:
|
||||
0----1
|
||||
| |
|
||||
| |
|
||||
3----2
|
||||
We can divide the vertices into two groups: {0, 2} and {1, 3}.
|
||||
```
|
||||
|
||||
```html
|
||||
Example 2:
|
||||
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
|
||||
Output: false
|
||||
Explanation:
|
||||
The graph looks like this:
|
||||
0----1
|
||||
| \ |
|
||||
| \ |
|
||||
3----2
|
||||
We cannot find a way to divide the set of nodes into two independent subsets.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isBipartite(int[][] graph) {
|
||||
int[] colors = new int[graph.length];
|
||||
Arrays.fill(colors, -1);
|
||||
for (int i = 0; i < graph.length; i++) { // 处理图不是连通的情况
|
||||
if (colors[i] == -1 && !isBipartite(i, 0, colors, graph)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isBipartite(int curNode, int curColor, int[] colors, int[][] graph) {
|
||||
if (colors[curNode] != -1) {
|
||||
return colors[curNode] == curColor;
|
||||
}
|
||||
colors[curNode] = curColor;
|
||||
for (int nextNode : graph[curNode]) {
|
||||
if (!isBipartite(nextNode, 1 - curColor, colors, graph)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 拓扑排序
|
||||
|
||||
常用于在具有先序关系的任务规划中。
|
||||
|
||||
## 课程安排的合法性
|
||||
|
||||
[207. Course Schedule (Medium)](https://leetcode.com/problems/course-schedule/description/)
|
||||
|
||||
```html
|
||||
2, [[1,0]]
|
||||
return true
|
||||
```
|
||||
|
||||
```html
|
||||
2, [[1,0],[0,1]]
|
||||
return false
|
||||
```
|
||||
|
||||
题目描述:一个课程可能会先修课程,判断给定的先修课程规定是否合法。
|
||||
|
||||
本题不需要使用拓扑排序,只需要检测有向图是否存在环即可。
|
||||
|
||||
```java
|
||||
public boolean canFinish(int numCourses, int[][] prerequisites) {
|
||||
List<Integer>[] graphic = new List[numCourses];
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
graphic[i] = new ArrayList<>();
|
||||
}
|
||||
for (int[] pre : prerequisites) {
|
||||
graphic[pre[0]].add(pre[1]);
|
||||
}
|
||||
boolean[] globalMarked = new boolean[numCourses];
|
||||
boolean[] localMarked = new boolean[numCourses];
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
if (hasCycle(globalMarked, localMarked, graphic, i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked,
|
||||
List<Integer>[] graphic, int curNode) {
|
||||
|
||||
if (localMarked[curNode]) {
|
||||
return true;
|
||||
}
|
||||
if (globalMarked[curNode]) {
|
||||
return false;
|
||||
}
|
||||
globalMarked[curNode] = true;
|
||||
localMarked[curNode] = true;
|
||||
for (int nextNode : graphic[curNode]) {
|
||||
if (hasCycle(globalMarked, localMarked, graphic, nextNode)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
localMarked[curNode] = false;
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
## 课程安排的顺序
|
||||
|
||||
[210. Course Schedule II (Medium)](https://leetcode.com/problems/course-schedule-ii/description/)
|
||||
|
||||
```html
|
||||
4, [[1,0],[2,0],[3,1],[3,2]]
|
||||
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].
|
||||
```
|
||||
|
||||
使用 DFS 来实现拓扑排序,使用一个栈存储后序遍历结果,这个栈的逆序结果就是拓扑排序结果。
|
||||
|
||||
证明:对于任何先序关系:v->w,后序遍历结果可以保证 w 先进入栈中,因此栈的逆序结果中 v 会在 w 之前。
|
||||
|
||||
```java
|
||||
public int[] findOrder(int numCourses, int[][] prerequisites) {
|
||||
List<Integer>[] graphic = new List[numCourses];
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
graphic[i] = new ArrayList<>();
|
||||
}
|
||||
for (int[] pre : prerequisites) {
|
||||
graphic[pre[0]].add(pre[1]);
|
||||
}
|
||||
Stack<Integer> postOrder = new Stack<>();
|
||||
boolean[] globalMarked = new boolean[numCourses];
|
||||
boolean[] localMarked = new boolean[numCourses];
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
if (hasCycle(globalMarked, localMarked, graphic, i, postOrder)) {
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
int[] orders = new int[numCourses];
|
||||
for (int i = numCourses - 1; i >= 0; i--) {
|
||||
orders[i] = postOrder.pop();
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked, List<Integer>[] graphic,
|
||||
int curNode, Stack<Integer> postOrder) {
|
||||
|
||||
if (localMarked[curNode]) {
|
||||
return true;
|
||||
}
|
||||
if (globalMarked[curNode]) {
|
||||
return false;
|
||||
}
|
||||
globalMarked[curNode] = true;
|
||||
localMarked[curNode] = true;
|
||||
for (int nextNode : graphic[curNode]) {
|
||||
if (hasCycle(globalMarked, localMarked, graphic, nextNode, postOrder)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
localMarked[curNode] = false;
|
||||
postOrder.push(curNode);
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
# 并查集
|
||||
|
||||
并查集可以动态地连通两个点,并且可以非常快速地判断两个点是否连通。
|
||||
|
||||
## 冗余连接
|
||||
|
||||
[684. Redundant Connection (Medium)](https://leetcode.com/problems/redundant-connection/description/)
|
||||
|
||||
```html
|
||||
Input: [[1,2], [1,3], [2,3]]
|
||||
Output: [2,3]
|
||||
Explanation: The given undirected graph will be like this:
|
||||
1
|
||||
/ \
|
||||
2 - 3
|
||||
```
|
||||
|
||||
题目描述:有一系列的边连成的图,找出一条边,移除它之后该图能够成为一棵树。
|
||||
|
||||
```java
|
||||
public int[] findRedundantConnection(int[][] edges) {
|
||||
int N = edges.length;
|
||||
UF uf = new UF(N);
|
||||
for (int[] e : edges) {
|
||||
int u = e[0], v = e[1];
|
||||
if (uf.connect(u, v)) {
|
||||
return e;
|
||||
}
|
||||
uf.union(u, v);
|
||||
}
|
||||
return new int[]{-1, -1};
|
||||
}
|
||||
|
||||
private class UF {
|
||||
|
||||
private int[] id;
|
||||
|
||||
UF(int N) {
|
||||
id = new int[N + 1];
|
||||
for (int i = 0; i < id.length; i++) {
|
||||
id[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
void union(int u, int v) {
|
||||
int uID = find(u);
|
||||
int vID = find(v);
|
||||
if (uID == vID) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < id.length; i++) {
|
||||
if (id[i] == uID) {
|
||||
id[i] = vID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int find(int p) {
|
||||
return id[p];
|
||||
}
|
||||
|
||||
boolean connect(int u, int v) {
|
||||
return find(u) == find(v);
|
||||
}
|
||||
}
|
||||
```
|
226
docs/notes/Leetcode 题解 - 字符串.md
Normal file
226
docs/notes/Leetcode 题解 - 字符串.md
Normal file
|
@ -0,0 +1,226 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [字符串循环移位包含](#字符串循环移位包含)
|
||||
* [字符串循环移位](#字符串循环移位)
|
||||
* [字符串中单词的翻转](#字符串中单词的翻转)
|
||||
* [两个字符串包含的字符是否完全相同](#两个字符串包含的字符是否完全相同)
|
||||
* [计算一组字符集合可以组成的回文字符串的最大长度](#计算一组字符集合可以组成的回文字符串的最大长度)
|
||||
* [字符串同构](#字符串同构)
|
||||
* [回文子字符串个数](#回文子字符串个数)
|
||||
* [判断一个整数是否是回文数](#判断一个整数是否是回文数)
|
||||
* [统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数](#统计二进制字符串中连续-1-和连续-0-数量相同的子字符串个数)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 字符串循环移位包含
|
||||
|
||||
[编程之美 3.1](#)
|
||||
|
||||
```html
|
||||
s1 = AABCD, s2 = CDAA
|
||||
Return : true
|
||||
```
|
||||
|
||||
给定两个字符串 s1 和 s2,要求判定 s2 是否能够被 s1 做循环移位得到的字符串包含。
|
||||
|
||||
s1 进行循环移位的结果是 s1s1 的子字符串,因此只要判断 s2 是否是 s1s1 的子字符串即可。
|
||||
|
||||
# 字符串循环移位
|
||||
|
||||
[编程之美 2.17](#)
|
||||
|
||||
```html
|
||||
s = "abcd123" k = 3
|
||||
Return "123abcd"
|
||||
```
|
||||
|
||||
将字符串向右循环移动 k 位。
|
||||
|
||||
将 abcd123 中的 abcd 和 123 单独翻转,得到 dcba321,然后对整个字符串进行翻转,得到 123abcd。
|
||||
|
||||
# 字符串中单词的翻转
|
||||
|
||||
[程序员代码面试指南](#)
|
||||
|
||||
```html
|
||||
s = "I am a student"
|
||||
Return "student a am I"
|
||||
```
|
||||
|
||||
将每个单词翻转,然后将整个字符串翻转。
|
||||
|
||||
# 两个字符串包含的字符是否完全相同
|
||||
|
||||
[242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/)
|
||||
|
||||
```html
|
||||
s = "anagram", t = "nagaram", return true.
|
||||
s = "rat", t = "car", return false.
|
||||
```
|
||||
|
||||
可以用 HashMap 来映射字符与出现次数,然后比较两个字符串出现的字符数量是否相同。
|
||||
|
||||
由于本题的字符串只包含 26 个小写字符,因此可以使用长度为 26 的整型数组对字符串出现的字符进行统计,不再使用 HashMap。
|
||||
|
||||
```java
|
||||
public boolean isAnagram(String s, String t) {
|
||||
int[] cnts = new int[26];
|
||||
for (char c : s.toCharArray()) {
|
||||
cnts[c - 'a']++;
|
||||
}
|
||||
for (char c : t.toCharArray()) {
|
||||
cnts[c - 'a']--;
|
||||
}
|
||||
for (int cnt : cnts) {
|
||||
if (cnt != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 计算一组字符集合可以组成的回文字符串的最大长度
|
||||
|
||||
[409. Longest Palindrome (Easy)](https://leetcode.com/problems/longest-palindrome/description/)
|
||||
|
||||
```html
|
||||
Input : "abccccdd"
|
||||
Output : 7
|
||||
Explanation : One longest palindrome that can be built is "dccaccd", whose length is 7.
|
||||
```
|
||||
|
||||
使用长度为 256 的整型数组来统计每个字符出现的个数,每个字符有偶数个可以用来构成回文字符串。
|
||||
|
||||
因为回文字符串最中间的那个字符可以单独出现,所以如果有单独的字符就把它放到最中间。
|
||||
|
||||
```java
|
||||
public int longestPalindrome(String s) {
|
||||
int[] cnts = new int[256];
|
||||
for (char c : s.toCharArray()) {
|
||||
cnts[c]++;
|
||||
}
|
||||
int palindrome = 0;
|
||||
for (int cnt : cnts) {
|
||||
palindrome += (cnt / 2) * 2;
|
||||
}
|
||||
if (palindrome < s.length()) {
|
||||
palindrome++; // 这个条件下 s 中一定有单个未使用的字符存在,可以把这个字符放到回文的最中间
|
||||
}
|
||||
return palindrome;
|
||||
}
|
||||
```
|
||||
|
||||
# 字符串同构
|
||||
|
||||
[205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/)
|
||||
|
||||
```html
|
||||
Given "egg", "add", return true.
|
||||
Given "foo", "bar", return false.
|
||||
Given "paper", "title", return true.
|
||||
```
|
||||
|
||||
记录一个字符上次出现的位置,如果两个字符串中的字符上次出现的位置一样,那么就属于同构。
|
||||
|
||||
```java
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
int[] preIndexOfS = new int[256];
|
||||
int[] preIndexOfT = new int[256];
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char sc = s.charAt(i), tc = t.charAt(i);
|
||||
if (preIndexOfS[sc] != preIndexOfT[tc]) {
|
||||
return false;
|
||||
}
|
||||
preIndexOfS[sc] = i + 1;
|
||||
preIndexOfT[tc] = i + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 回文子字符串个数
|
||||
|
||||
[647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/)
|
||||
|
||||
```html
|
||||
Input: "aaa"
|
||||
Output: 6
|
||||
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
|
||||
```
|
||||
|
||||
从字符串的某一位开始,尝试着去扩展子字符串。
|
||||
|
||||
```java
|
||||
private int cnt = 0;
|
||||
|
||||
public int countSubstrings(String s) {
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
extendSubstrings(s, i, i); // 奇数长度
|
||||
extendSubstrings(s, i, i + 1); // 偶数长度
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
private void extendSubstrings(String s, int start, int end) {
|
||||
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
|
||||
start--;
|
||||
end++;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 判断一个整数是否是回文数
|
||||
|
||||
[9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/)
|
||||
|
||||
要求不能使用额外空间,也就不能将整数转换为字符串进行判断。
|
||||
|
||||
将整数分成左右两部分,右边那部分需要转置,然后判断这两部分是否相等。
|
||||
|
||||
```java
|
||||
public boolean isPalindrome(int x) {
|
||||
if (x == 0) {
|
||||
return true;
|
||||
}
|
||||
if (x < 0 || x % 10 == 0) {
|
||||
return false;
|
||||
}
|
||||
int right = 0;
|
||||
while (x > right) {
|
||||
right = right * 10 + x % 10;
|
||||
x /= 10;
|
||||
}
|
||||
return x == right || x == right / 10;
|
||||
}
|
||||
```
|
||||
|
||||
# 统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数
|
||||
|
||||
[696. Count Binary Substrings (Easy)](https://leetcode.com/problems/count-binary-substrings/description/)
|
||||
|
||||
```html
|
||||
Input: "00110011"
|
||||
Output: 6
|
||||
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
|
||||
```
|
||||
|
||||
```java
|
||||
public int countBinarySubstrings(String s) {
|
||||
int preLen = 0, curLen = 1, count = 0;
|
||||
for (int i = 1; i < s.length(); i++) {
|
||||
if (s.charAt(i) == s.charAt(i - 1)) {
|
||||
curLen++;
|
||||
} else {
|
||||
preLen = curLen;
|
||||
curLen = 1;
|
||||
}
|
||||
|
||||
if (preLen >= curLen) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
227
docs/notes/Leetcode 题解 - 排序.md
Normal file
227
docs/notes/Leetcode 题解 - 排序.md
Normal file
|
@ -0,0 +1,227 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [快速选择](#快速选择)
|
||||
* [堆排序](#堆排序)
|
||||
* [Kth Element](#kth-element)
|
||||
* [桶排序](#桶排序)
|
||||
* [出现频率最多的 k 个数](#出现频率最多的-k-个数)
|
||||
* [按照字符出现次数对字符串排序](#按照字符出现次数对字符串排序)
|
||||
* [荷兰国旗问题](#荷兰国旗问题)
|
||||
* [按颜色进行排序](#按颜色进行排序)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 快速选择
|
||||
|
||||
用于求解 **Kth Element** 问题,使用快速排序的 partition() 进行实现。
|
||||
|
||||
需要先打乱数组,否则最坏情况下时间复杂度为 O(N<sup>2</sup>)。
|
||||
|
||||
# 堆排序
|
||||
|
||||
用于求解 **TopK Elements** 问题,通过维护一个大小为 K 的堆,堆中的元素就是 TopK Elements。
|
||||
|
||||
堆排序也可以用于求解 Kth Element 问题,堆顶元素就是 Kth Element。
|
||||
|
||||
快速选择也可以求解 TopK Elements 问题,因为找到 Kth Element 之后,再遍历一次数组,所有小于等于 Kth Element 的元素都是 TopK Elements。
|
||||
|
||||
可以看到,快速选择和堆排序都可以求解 Kth Element 和 TopK Elements 问题。
|
||||
|
||||
## Kth Element
|
||||
|
||||
[215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/)
|
||||
|
||||
题目描述:找到第 k 大的元素。
|
||||
|
||||
**排序** :时间复杂度 O(NlogN),空间复杂度 O(1)
|
||||
|
||||
```java
|
||||
public int findKthLargest(int[] nums, int k) {
|
||||
Arrays.sort(nums);
|
||||
return nums[nums.length - k];
|
||||
}
|
||||
```
|
||||
|
||||
**堆排序** :时间复杂度 O(NlogK),空间复杂度 O(K)。
|
||||
|
||||
```java
|
||||
public int findKthLargest(int[] nums, int k) {
|
||||
PriorityQueue<Integer> pq = new PriorityQueue<>(); // 小顶堆
|
||||
for (int val : nums) {
|
||||
pq.add(val);
|
||||
if (pq.size() > k) // 维护堆的大小为 K
|
||||
pq.poll();
|
||||
}
|
||||
return pq.peek();
|
||||
}
|
||||
```
|
||||
|
||||
**快速选择** :时间复杂度 O(N),空间复杂度 O(1)
|
||||
|
||||
```java
|
||||
public int findKthLargest(int[] nums, int k) {
|
||||
k = nums.length - k;
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l < h) {
|
||||
int j = partition(nums, l, h);
|
||||
if (j == k) {
|
||||
break;
|
||||
} else if (j < k) {
|
||||
l = j + 1;
|
||||
} else {
|
||||
h = j - 1;
|
||||
}
|
||||
}
|
||||
return nums[k];
|
||||
}
|
||||
|
||||
private int partition(int[] a, int l, int h) {
|
||||
int i = l, j = h + 1;
|
||||
while (true) {
|
||||
while (a[++i] < a[l] && i < h) ;
|
||||
while (a[--j] > a[l] && j > l) ;
|
||||
if (i >= j) {
|
||||
break;
|
||||
}
|
||||
swap(a, i, j);
|
||||
}
|
||||
swap(a, l, j);
|
||||
return j;
|
||||
}
|
||||
|
||||
private void swap(int[] a, int i, int j) {
|
||||
int t = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = t;
|
||||
}
|
||||
```
|
||||
|
||||
# 桶排序
|
||||
|
||||
## 出现频率最多的 k 个数
|
||||
|
||||
[347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/)
|
||||
|
||||
```html
|
||||
Given [1,1,1,2,2,3] and k = 2, return [1,2].
|
||||
```
|
||||
|
||||
设置若干个桶,每个桶存储出现频率相同的数,并且桶的下标代表桶中数出现的频率,即第 i 个桶中存储的数出现的频率为 i。
|
||||
|
||||
把数都放到桶之后,从后向前遍历桶,最先得到的 k 个数就是出现频率最多的的 k 个数。
|
||||
|
||||
```java
|
||||
public List<Integer> topKFrequent(int[] nums, int k) {
|
||||
Map<Integer, Integer> frequencyForNum = new HashMap<>();
|
||||
for (int num : nums) {
|
||||
frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1);
|
||||
}
|
||||
List<Integer>[] buckets = new ArrayList[nums.length + 1];
|
||||
for (int key : frequencyForNum.keySet()) {
|
||||
int frequency = frequencyForNum.get(key);
|
||||
if (buckets[frequency] == null) {
|
||||
buckets[frequency] = new ArrayList<>();
|
||||
}
|
||||
buckets[frequency].add(key);
|
||||
}
|
||||
List<Integer> topK = new ArrayList<>();
|
||||
for (int i = buckets.length - 1; i >= 0 && topK.size() < k; i--) {
|
||||
if (buckets[i] == null) {
|
||||
continue;
|
||||
}
|
||||
if (buckets[i].size() <= (k - topK.size())) {
|
||||
topK.addAll(buckets[i]);
|
||||
} else {
|
||||
topK.addAll(buckets[i].subList(0, k - topK.size()));
|
||||
}
|
||||
}
|
||||
return topK;
|
||||
}
|
||||
```
|
||||
|
||||
## 按照字符出现次数对字符串排序
|
||||
|
||||
[451. Sort Characters By Frequency (Medium)](https://leetcode.com/problems/sort-characters-by-frequency/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
"tree"
|
||||
|
||||
Output:
|
||||
"eert"
|
||||
|
||||
Explanation:
|
||||
'e' appears twice while 'r' and 't' both appear once.
|
||||
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
|
||||
```
|
||||
|
||||
```java
|
||||
public String frequencySort(String s) {
|
||||
Map<Character, Integer> frequencyForNum = new HashMap<>();
|
||||
for (char c : s.toCharArray())
|
||||
frequencyForNum.put(c, frequencyForNum.getOrDefault(c, 0) + 1);
|
||||
|
||||
List<Character>[] frequencyBucket = new ArrayList[s.length() + 1];
|
||||
for (char c : frequencyForNum.keySet()) {
|
||||
int f = frequencyForNum.get(c);
|
||||
if (frequencyBucket[f] == null) {
|
||||
frequencyBucket[f] = new ArrayList<>();
|
||||
}
|
||||
frequencyBucket[f].add(c);
|
||||
}
|
||||
StringBuilder str = new StringBuilder();
|
||||
for (int i = frequencyBucket.length - 1; i >= 0; i--) {
|
||||
if (frequencyBucket[i] == null) {
|
||||
continue;
|
||||
}
|
||||
for (char c : frequencyBucket[i]) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
str.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
```
|
||||
|
||||
# 荷兰国旗问题
|
||||
|
||||
荷兰国旗包含三种颜色:红、白、蓝。
|
||||
|
||||
有三种颜色的球,算法的目标是将这三种球按颜色顺序正确地排列。
|
||||
|
||||
它其实是三向切分快速排序的一种变种,在三向切分快速排序中,每次切分都将数组分成三个区间:小于切分元素、等于切分元素、大于切分元素,而该算法是将数组分成三个区间:等于红色、等于白色、等于蓝色。
|
||||
|
||||
<div align="center"> <img src="pics/7a3215ec-6fb7-4935-8b0d-cb408208f7cb.png"/> </div><br>
|
||||
|
||||
## 按颜色进行排序
|
||||
|
||||
[75. Sort Colors (Medium)](https://leetcode.com/problems/sort-colors/description/)
|
||||
|
||||
```html
|
||||
Input: [2,0,2,1,1,0]
|
||||
Output: [0,0,1,1,2,2]
|
||||
```
|
||||
|
||||
题目描述:只有 0/1/2 三种颜色。
|
||||
|
||||
```java
|
||||
public void sortColors(int[] nums) {
|
||||
int zero = -1, one = 0, two = nums.length;
|
||||
while (one < two) {
|
||||
if (nums[one] == 0) {
|
||||
swap(nums, ++zero, one++);
|
||||
} else if (nums[one] == 2) {
|
||||
swap(nums, --two, one);
|
||||
} else {
|
||||
++one;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void swap(int[] nums, int i, int j) {
|
||||
int t = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = t;
|
||||
}
|
||||
```
|
1269
docs/notes/Leetcode 题解 - 搜索.md
Normal file
1269
docs/notes/Leetcode 题解 - 搜索.md
Normal file
File diff suppressed because it is too large
Load Diff
510
docs/notes/Leetcode 题解 - 数学.md
Normal file
510
docs/notes/Leetcode 题解 - 数学.md
Normal file
|
@ -0,0 +1,510 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [素数分解](#素数分解)
|
||||
* [整除](#整除)
|
||||
* [最大公约数最小公倍数](#最大公约数最小公倍数)
|
||||
* [生成素数序列](#生成素数序列)
|
||||
* [最大公约数](#最大公约数)
|
||||
* [使用位操作和减法求解最大公约数](#使用位操作和减法求解最大公约数)
|
||||
* [进制转换](#进制转换)
|
||||
* [7 进制](#7-进制)
|
||||
* [16 进制](#16-进制)
|
||||
* [26 进制](#26-进制)
|
||||
* [阶乘](#阶乘)
|
||||
* [统计阶乘尾部有多少个 0](#统计阶乘尾部有多少个-0)
|
||||
* [字符串加法减法](#字符串加法减法)
|
||||
* [二进制加法](#二进制加法)
|
||||
* [字符串加法](#字符串加法)
|
||||
* [相遇问题](#相遇问题)
|
||||
* [改变数组元素使所有的数组元素都相等](#改变数组元素使所有的数组元素都相等)
|
||||
* [解法 1](#解法-1)
|
||||
* [解法 2](#解法-2)
|
||||
* [多数投票问题](#多数投票问题)
|
||||
* [数组中出现次数多于 n / 2 的元素](#数组中出现次数多于-n--2-的元素)
|
||||
* [其它](#其它)
|
||||
* [平方数](#平方数)
|
||||
* [3 的 n 次方](#3-的-n-次方)
|
||||
* [乘积数组](#乘积数组)
|
||||
* [找出数组中的乘积最大的三个数](#找出数组中的乘积最大的三个数)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 素数分解
|
||||
|
||||
每一个数都可以分解成素数的乘积,例如 84 = 2<sup>2</sup> \* 3<sup>1</sup> \* 5<sup>0</sup> \* 7<sup>1</sup> \* 11<sup>0</sup> \* 13<sup>0</sup> \* 17<sup>0</sup> \* …
|
||||
|
||||
# 整除
|
||||
|
||||
令 x = 2<sup>m0</sup> \* 3<sup>m1</sup> \* 5<sup>m2</sup> \* 7<sup>m3</sup> \* 11<sup>m4</sup> \* …
|
||||
|
||||
令 y = 2<sup>n0</sup> \* 3<sup>n1</sup> \* 5<sup>n2</sup> \* 7<sup>n3</sup> \* 11<sup>n4</sup> \* …
|
||||
|
||||
如果 x 整除 y(y mod x == 0),则对于所有 i,mi <= ni。
|
||||
|
||||
## 最大公约数最小公倍数
|
||||
|
||||
x 和 y 的最大公约数为:gcd(x,y) = 2<sup>min(m0,n0)</sup> \* 3<sup>min(m1,n1)</sup> \* 5<sup>min(m2,n2)</sup> \* ...
|
||||
|
||||
x 和 y 的最小公倍数为:lcm(x,y) = 2<sup>max(m0,n0)</sup> \* 3<sup>max(m1,n1)</sup> \* 5<sup>max(m2,n2)</sup> \* ...
|
||||
|
||||
## 生成素数序列
|
||||
|
||||
[204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/)
|
||||
|
||||
埃拉托斯特尼筛法在每次找到一个素数时,将能被素数整除的数排除掉。
|
||||
|
||||
```java
|
||||
public int countPrimes(int n) {
|
||||
boolean[] notPrimes = new boolean[n + 1];
|
||||
int count = 0;
|
||||
for (int i = 2; i < n; i++) {
|
||||
if (notPrimes[i]) {
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
// 从 i * i 开始,因为如果 k < i,那么 k * i 在之前就已经被去除过了
|
||||
for (long j = (long) (i) * i; j < n; j += i) {
|
||||
notPrimes[(int) j] = true;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 最大公约数
|
||||
|
||||
```java
|
||||
int gcd(int a, int b) {
|
||||
return b == 0 ? a : gcd(b, a % b);
|
||||
}
|
||||
```
|
||||
|
||||
最小公倍数为两数的乘积除以最大公约数。
|
||||
|
||||
```java
|
||||
int lcm(int a, int b) {
|
||||
return a * b / gcd(a, b);
|
||||
}
|
||||
```
|
||||
|
||||
## 使用位操作和减法求解最大公约数
|
||||
|
||||
[编程之美:2.7](#)
|
||||
|
||||
对于 a 和 b 的最大公约数 f(a, b),有:
|
||||
|
||||
- 如果 a 和 b 均为偶数,f(a, b) = 2\*f(a/2, b/2);
|
||||
- 如果 a 是偶数 b 是奇数,f(a, b) = f(a/2, b);
|
||||
- 如果 b 是偶数 a 是奇数,f(a, b) = f(a, b/2);
|
||||
- 如果 a 和 b 均为奇数,f(a, b) = f(b, a-b);
|
||||
|
||||
乘 2 和除 2 都可以转换为移位操作。
|
||||
|
||||
```java
|
||||
public int gcd(int a, int b) {
|
||||
if (a < b) {
|
||||
return gcd(b, a);
|
||||
}
|
||||
if (b == 0) {
|
||||
return a;
|
||||
}
|
||||
boolean isAEven = isEven(a), isBEven = isEven(b);
|
||||
if (isAEven && isBEven) {
|
||||
return 2 * gcd(a >> 1, b >> 1);
|
||||
} else if (isAEven && !isBEven) {
|
||||
return gcd(a >> 1, b);
|
||||
} else if (!isAEven && isBEven) {
|
||||
return gcd(a, b >> 1);
|
||||
} else {
|
||||
return gcd(b, a - b);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 进制转换
|
||||
|
||||
## 7 进制
|
||||
|
||||
[504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/)
|
||||
|
||||
```java
|
||||
public String convertToBase7(int num) {
|
||||
if (num == 0) {
|
||||
return "0";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean isNegative = num < 0;
|
||||
if (isNegative) {
|
||||
num = -num;
|
||||
}
|
||||
while (num > 0) {
|
||||
sb.append(num % 7);
|
||||
num /= 7;
|
||||
}
|
||||
String ret = sb.reverse().toString();
|
||||
return isNegative ? "-" + ret : ret;
|
||||
}
|
||||
```
|
||||
|
||||
Java 中 static String toString(int num, int radix) 可以将一个整数转换为 radix 进制表示的字符串。
|
||||
|
||||
```java
|
||||
public String convertToBase7(int num) {
|
||||
return Integer.toString(num, 7);
|
||||
}
|
||||
```
|
||||
|
||||
## 16 进制
|
||||
|
||||
[405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
26
|
||||
|
||||
Output:
|
||||
"1a"
|
||||
|
||||
Input:
|
||||
-1
|
||||
|
||||
Output:
|
||||
"ffffffff"
|
||||
```
|
||||
|
||||
负数要用它的补码形式。
|
||||
|
||||
```java
|
||||
public String toHex(int num) {
|
||||
char[] map = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
if (num == 0) return "0";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (num != 0) {
|
||||
sb.append(map[num & 0b1111]);
|
||||
num >>>= 4; // 因为考虑的是补码形式,因此符号位就不能有特殊的意义,需要使用无符号右移,左边填 0
|
||||
}
|
||||
return sb.reverse().toString();
|
||||
}
|
||||
```
|
||||
|
||||
## 26 进制
|
||||
|
||||
[168. Excel Sheet Column Title (Easy)](https://leetcode.com/problems/excel-sheet-column-title/description/)
|
||||
|
||||
```html
|
||||
1 -> A
|
||||
2 -> B
|
||||
3 -> C
|
||||
...
|
||||
26 -> Z
|
||||
27 -> AA
|
||||
28 -> AB
|
||||
```
|
||||
|
||||
因为是从 1 开始计算的,而不是从 0 开始,因此需要对 n 执行 -1 操作。
|
||||
|
||||
```java
|
||||
public String convertToTitle(int n) {
|
||||
if (n == 0) {
|
||||
return "";
|
||||
}
|
||||
n--;
|
||||
return convertToTitle(n / 26) + (char) (n % 26 + 'A');
|
||||
}
|
||||
```
|
||||
|
||||
# 阶乘
|
||||
|
||||
## 统计阶乘尾部有多少个 0
|
||||
|
||||
[172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/)
|
||||
|
||||
尾部的 0 由 2 * 5 得来,2 的数量明显多于 5 的数量,因此只要统计有多少个 5 即可。
|
||||
|
||||
对于一个数 N,它所包含 5 的个数为:N/5 + N/5<sup>2</sup> + N/5<sup>3</sup> + ...,其中 N/5 表示不大于 N 的数中 5 的倍数贡献一个 5,N/5<sup>2</sup> 表示不大于 N 的数中 5<sup>2</sup> 的倍数再贡献一个 5 ...。
|
||||
|
||||
```java
|
||||
public int trailingZeroes(int n) {
|
||||
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
|
||||
}
|
||||
```
|
||||
|
||||
如果统计的是 N! 的二进制表示中最低位 1 的位置,只要统计有多少个 2 即可,该题目出自 [编程之美:2.2](#) 。和求解有多少个 5 一样,2 的个数为 N/2 + N/2<sup>2</sup> + N/2<sup>3</sup> + ...
|
||||
|
||||
# 字符串加法减法
|
||||
|
||||
## 二进制加法
|
||||
|
||||
[67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/)
|
||||
|
||||
```html
|
||||
a = "11"
|
||||
b = "1"
|
||||
Return "100".
|
||||
```
|
||||
|
||||
```java
|
||||
public String addBinary(String a, String b) {
|
||||
int i = a.length() - 1, j = b.length() - 1, carry = 0;
|
||||
StringBuilder str = new StringBuilder();
|
||||
while (carry == 1 || i >= 0 || j >= 0) {
|
||||
if (i >= 0 && a.charAt(i--) == '1') {
|
||||
carry++;
|
||||
}
|
||||
if (j >= 0 && b.charAt(j--) == '1') {
|
||||
carry++;
|
||||
}
|
||||
str.append(carry % 2);
|
||||
carry /= 2;
|
||||
}
|
||||
return str.reverse().toString();
|
||||
}
|
||||
```
|
||||
|
||||
## 字符串加法
|
||||
|
||||
[415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/)
|
||||
|
||||
字符串的值为非负整数。
|
||||
|
||||
```java
|
||||
public String addStrings(String num1, String num2) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
int carry = 0, i = num1.length() - 1, j = num2.length() - 1;
|
||||
while (carry == 1 || i >= 0 || j >= 0) {
|
||||
int x = i < 0 ? 0 : num1.charAt(i--) - '0';
|
||||
int y = j < 0 ? 0 : num2.charAt(j--) - '0';
|
||||
str.append((x + y + carry) % 10);
|
||||
carry = (x + y + carry) / 10;
|
||||
}
|
||||
return str.reverse().toString();
|
||||
}
|
||||
```
|
||||
|
||||
# 相遇问题
|
||||
|
||||
## 改变数组元素使所有的数组元素都相等
|
||||
|
||||
[462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
[1,2,3]
|
||||
|
||||
Output:
|
||||
2
|
||||
|
||||
Explanation:
|
||||
Only two moves are needed (remember each move increments or decrements one element):
|
||||
|
||||
[1,2,3] => [2,2,3] => [2,2,2]
|
||||
```
|
||||
|
||||
每次可以对一个数组元素加一或者减一,求最小的改变次数。
|
||||
|
||||
这是个典型的相遇问题,移动距离最小的方式是所有元素都移动到中位数。理由如下:
|
||||
|
||||
设 m 为中位数。a 和 b 是 m 两边的两个元素,且 b > a。要使 a 和 b 相等,它们总共移动的次数为 b - a,这个值等于 (b - m) + (m - a),也就是把这两个数移动到中位数的移动次数。
|
||||
|
||||
设数组长度为 N,则可以找到 N/2 对 a 和 b 的组合,使它们都移动到 m 的位置。
|
||||
|
||||
## 解法 1
|
||||
|
||||
先排序,时间复杂度:O(NlogN)
|
||||
|
||||
```java
|
||||
public int minMoves2(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
int move = 0;
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l <= h) {
|
||||
move += nums[h] - nums[l];
|
||||
l++;
|
||||
h--;
|
||||
}
|
||||
return move;
|
||||
}
|
||||
```
|
||||
|
||||
## 解法 2
|
||||
|
||||
使用快速选择找到中位数,时间复杂度 O(N)
|
||||
|
||||
```java
|
||||
public int minMoves2(int[] nums) {
|
||||
int move = 0;
|
||||
int median = findKthSmallest(nums, nums.length / 2);
|
||||
for (int num : nums) {
|
||||
move += Math.abs(num - median);
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
private int findKthSmallest(int[] nums, int k) {
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l < h) {
|
||||
int j = partition(nums, l, h);
|
||||
if (j == k) {
|
||||
break;
|
||||
}
|
||||
if (j < k) {
|
||||
l = j + 1;
|
||||
} else {
|
||||
h = j - 1;
|
||||
}
|
||||
}
|
||||
return nums[k];
|
||||
}
|
||||
|
||||
private int partition(int[] nums, int l, int h) {
|
||||
int i = l, j = h + 1;
|
||||
while (true) {
|
||||
while (nums[++i] < nums[l] && i < h) ;
|
||||
while (nums[--j] > nums[l] && j > l) ;
|
||||
if (i >= j) {
|
||||
break;
|
||||
}
|
||||
swap(nums, i, j);
|
||||
}
|
||||
swap(nums, l, j);
|
||||
return j;
|
||||
}
|
||||
|
||||
private void swap(int[] nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
```
|
||||
|
||||
# 多数投票问题
|
||||
|
||||
## 数组中出现次数多于 n / 2 的元素
|
||||
|
||||
[169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/)
|
||||
|
||||
先对数组排序,最中间那个数出现次数一定多于 n / 2。
|
||||
|
||||
```java
|
||||
public int majorityElement(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
return nums[nums.length / 2];
|
||||
}
|
||||
```
|
||||
|
||||
可以利用 Boyer-Moore Majority Vote Algorithm 来解决这个问题,使得时间复杂度为 O(N)。可以这么理解该算法:使用 cnt 来统计一个元素出现的次数,当遍历到的元素和统计元素不相等时,令 cnt--。如果前面查找了 i 个元素,且 cnt == 0,说明前 i 个元素没有 majority,或者有 majority,但是出现的次数少于 i / 2,因为如果多于 i / 2 的话 cnt 就一定不会为 0。此时剩下的 n - i 个元素中,majority 的数目依然多于 (n - i) / 2,因此继续查找就能找出 majority。
|
||||
|
||||
```java
|
||||
public int majorityElement(int[] nums) {
|
||||
int cnt = 0, majority = nums[0];
|
||||
for (int num : nums) {
|
||||
majority = (cnt == 0) ? num : majority;
|
||||
cnt = (majority == num) ? cnt + 1 : cnt - 1;
|
||||
}
|
||||
return majority;
|
||||
}
|
||||
```
|
||||
|
||||
# 其它
|
||||
|
||||
## 平方数
|
||||
|
||||
[367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/)
|
||||
|
||||
```html
|
||||
Input: 16
|
||||
Returns: True
|
||||
```
|
||||
|
||||
平方序列:1,4,9,16,..
|
||||
|
||||
间隔:3,5,7,...
|
||||
|
||||
间隔为等差数列,使用这个特性可以得到从 1 开始的平方序列。
|
||||
|
||||
```java
|
||||
public boolean isPerfectSquare(int num) {
|
||||
int subNum = 1;
|
||||
while (num > 0) {
|
||||
num -= subNum;
|
||||
subNum += 2;
|
||||
}
|
||||
return num == 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 3 的 n 次方
|
||||
|
||||
[326. Power of Three (Easy)](https://leetcode.com/problems/power-of-three/description/)
|
||||
|
||||
```java
|
||||
public boolean isPowerOfThree(int n) {
|
||||
return n > 0 && (1162261467 % n == 0);
|
||||
}
|
||||
```
|
||||
|
||||
## 乘积数组
|
||||
|
||||
[238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/)
|
||||
|
||||
```html
|
||||
For example, given [1,2,3,4], return [24,12,8,6].
|
||||
```
|
||||
|
||||
给定一个数组,创建一个新数组,新数组的每个元素为原始数组中除了该位置上的元素之外所有元素的乘积。
|
||||
|
||||
要求时间复杂度为 O(N),并且不能使用除法。
|
||||
|
||||
```java
|
||||
public int[] productExceptSelf(int[] nums) {
|
||||
int n = nums.length;
|
||||
int[] products = new int[n];
|
||||
Arrays.fill(products, 1);
|
||||
int left = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
left *= nums[i - 1];
|
||||
products[i] *= left;
|
||||
}
|
||||
int right = 1;
|
||||
for (int i = n - 2; i >= 0; i--) {
|
||||
right *= nums[i + 1];
|
||||
products[i] *= right;
|
||||
}
|
||||
return products;
|
||||
}
|
||||
```
|
||||
|
||||
## 找出数组中的乘积最大的三个数
|
||||
|
||||
[628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/)
|
||||
|
||||
```html
|
||||
Input: [1,2,3,4]
|
||||
Output: 24
|
||||
```
|
||||
|
||||
```java
|
||||
public int maximumProduct(int[] nums) {
|
||||
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
|
||||
for (int n : nums) {
|
||||
if (n > max1) {
|
||||
max3 = max2;
|
||||
max2 = max1;
|
||||
max1 = n;
|
||||
} else if (n > max2) {
|
||||
max3 = max2;
|
||||
max2 = n;
|
||||
} else if (n > max3) {
|
||||
max3 = n;
|
||||
}
|
||||
|
||||
if (n < min1) {
|
||||
min2 = min1;
|
||||
min1 = n;
|
||||
} else if (n < min2) {
|
||||
min2 = n;
|
||||
}
|
||||
}
|
||||
return Math.max(max1*max2*max3, max1*min1*min2);
|
||||
}
|
||||
```
|
434
docs/notes/Leetcode 题解 - 数组与矩阵.md
Normal file
434
docs/notes/Leetcode 题解 - 数组与矩阵.md
Normal file
|
@ -0,0 +1,434 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [把数组中的 0 移到末尾](#把数组中的-0-移到末尾)
|
||||
* [改变矩阵维度](#改变矩阵维度)
|
||||
* [找出数组中最长的连续 1](#找出数组中最长的连续-1)
|
||||
* [有序矩阵查找](#有序矩阵查找)
|
||||
* [有序矩阵的 Kth Element](#有序矩阵的-kth-element)
|
||||
* [一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数](#一个数组元素在-[1,-n]-之间,其中一个数被替换为另一个数,找出重复的数和丢失的数)
|
||||
* [找出数组中重复的数,数组值在 [1, n] 之间](#找出数组中重复的数,数组值在-[1,-n]-之间)
|
||||
* [数组相邻差值的个数](#数组相邻差值的个数)
|
||||
* [数组的度](#数组的度)
|
||||
* [对角元素相等的矩阵](#对角元素相等的矩阵)
|
||||
* [嵌套数组](#嵌套数组)
|
||||
* [分隔数组](#分隔数组)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 把数组中的 0 移到末尾
|
||||
|
||||
[283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/)
|
||||
|
||||
```html
|
||||
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
|
||||
```
|
||||
|
||||
```java
|
||||
public void moveZeroes(int[] nums) {
|
||||
int idx = 0;
|
||||
for (int num : nums) {
|
||||
if (num != 0) {
|
||||
nums[idx++] = num;
|
||||
}
|
||||
}
|
||||
while (idx < nums.length) {
|
||||
nums[idx++] = 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 改变矩阵维度
|
||||
|
||||
[566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
nums =
|
||||
[[1,2],
|
||||
[3,4]]
|
||||
r = 1, c = 4
|
||||
|
||||
Output:
|
||||
[[1,2,3,4]]
|
||||
|
||||
Explanation:
|
||||
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
|
||||
```
|
||||
|
||||
```java
|
||||
public int[][] matrixReshape(int[][] nums, int r, int c) {
|
||||
int m = nums.length, n = nums[0].length;
|
||||
if (m * n != r * c) {
|
||||
return nums;
|
||||
}
|
||||
int[][] reshapedNums = new int[r][c];
|
||||
int index = 0;
|
||||
for (int i = 0; i < r; i++) {
|
||||
for (int j = 0; j < c; j++) {
|
||||
reshapedNums[i][j] = nums[index / n][index % n];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return reshapedNums;
|
||||
}
|
||||
```
|
||||
|
||||
# 找出数组中最长的连续 1
|
||||
|
||||
[485. Max Consecutive Ones (Easy)](https://leetcode.com/problems/max-consecutive-ones/description/)
|
||||
|
||||
```java
|
||||
public int findMaxConsecutiveOnes(int[] nums) {
|
||||
int max = 0, cur = 0;
|
||||
for (int x : nums) {
|
||||
cur = x == 0 ? 0 : cur + 1;
|
||||
max = Math.max(max, cur);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
# 有序矩阵查找
|
||||
|
||||
[240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/)
|
||||
|
||||
```html
|
||||
[
|
||||
[ 1, 5, 9],
|
||||
[10, 11, 13],
|
||||
[12, 13, 15]
|
||||
]
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean searchMatrix(int[][] matrix, int target) {
|
||||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
|
||||
int m = matrix.length, n = matrix[0].length;
|
||||
int row = 0, col = n - 1;
|
||||
while (row < m && col >= 0) {
|
||||
if (target == matrix[row][col]) return true;
|
||||
else if (target < matrix[row][col]) col--;
|
||||
else row++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
# 有序矩阵的 Kth Element
|
||||
|
||||
[378. Kth Smallest Element in a Sorted Matrix ((Medium))](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)
|
||||
|
||||
```html
|
||||
matrix = [
|
||||
[ 1, 5, 9],
|
||||
[10, 11, 13],
|
||||
[12, 13, 15]
|
||||
],
|
||||
k = 8,
|
||||
|
||||
return 13.
|
||||
```
|
||||
|
||||
解题参考:[Share my thoughts and Clean Java Code](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173)
|
||||
|
||||
二分查找解法:
|
||||
|
||||
```java
|
||||
public int kthSmallest(int[][] matrix, int k) {
|
||||
int m = matrix.length, n = matrix[0].length;
|
||||
int lo = matrix[0][0], hi = matrix[m - 1][n - 1];
|
||||
while (lo <= hi) {
|
||||
int mid = lo + (hi - lo) / 2;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n && matrix[i][j] <= mid; j++) {
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
if (cnt < k) lo = mid + 1;
|
||||
else hi = mid - 1;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
```
|
||||
|
||||
堆解法:
|
||||
|
||||
```java
|
||||
public int kthSmallest(int[][] matrix, int k) {
|
||||
int m = matrix.length, n = matrix[0].length;
|
||||
PriorityQueue<Tuple> pq = new PriorityQueue<Tuple>();
|
||||
for(int j = 0; j < n; j++) pq.offer(new Tuple(0, j, matrix[0][j]));
|
||||
for(int i = 0; i < k - 1; i++) { // 小根堆,去掉 k - 1 个堆顶元素,此时堆顶元素就是第 k 的数
|
||||
Tuple t = pq.poll();
|
||||
if(t.x == m - 1) continue;
|
||||
pq.offer(new Tuple(t.x + 1, t.y, matrix[t.x + 1][t.y]));
|
||||
}
|
||||
return pq.poll().val;
|
||||
}
|
||||
|
||||
class Tuple implements Comparable<Tuple> {
|
||||
int x, y, val;
|
||||
public Tuple(int x, int y, int val) {
|
||||
this.x = x; this.y = y; this.val = val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Tuple that) {
|
||||
return this.val - that.val;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数
|
||||
|
||||
[645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/)
|
||||
|
||||
```html
|
||||
Input: nums = [1,2,2,4]
|
||||
Output: [2,3]
|
||||
```
|
||||
|
||||
```html
|
||||
Input: nums = [1,2,2,4]
|
||||
Output: [2,3]
|
||||
```
|
||||
|
||||
最直接的方法是先对数组进行排序,这种方法时间复杂度为 O(NlogN)。本题可以以 O(N) 的时间复杂度、O(1) 空间复杂度来求解。
|
||||
|
||||
主要思想是通过交换数组元素,使得数组上的元素在正确的位置上。
|
||||
|
||||
```java
|
||||
public int[] findErrorNums(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
while (nums[i] != i + 1 && nums[nums[i] - 1] != nums[i]) {
|
||||
swap(nums, i, nums[i] - 1);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] != i + 1) {
|
||||
return new int[]{nums[i], i + 1};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void swap(int[] nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
```
|
||||
|
||||
类似题目:
|
||||
|
||||
- [448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/),寻找所有丢失的元素
|
||||
- [442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/),寻找所有重复的元素。
|
||||
|
||||
# 找出数组中重复的数,数组值在 [1, n] 之间
|
||||
|
||||
[287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/)
|
||||
|
||||
要求不能修改数组,也不能使用额外的空间。
|
||||
|
||||
二分查找解法:
|
||||
|
||||
```java
|
||||
public int findDuplicate(int[] nums) {
|
||||
int l = 1, h = nums.length - 1;
|
||||
while (l <= h) {
|
||||
int mid = l + (h - l) / 2;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] <= mid) cnt++;
|
||||
}
|
||||
if (cnt > mid) h = mid - 1;
|
||||
else l = mid + 1;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
```
|
||||
|
||||
双指针解法,类似于有环链表中找出环的入口:
|
||||
|
||||
```java
|
||||
public int findDuplicate(int[] nums) {
|
||||
int slow = nums[0], fast = nums[nums[0]];
|
||||
while (slow != fast) {
|
||||
slow = nums[slow];
|
||||
fast = nums[nums[fast]];
|
||||
}
|
||||
fast = 0;
|
||||
while (slow != fast) {
|
||||
slow = nums[slow];
|
||||
fast = nums[fast];
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
```
|
||||
|
||||
# 数组相邻差值的个数
|
||||
|
||||
[667. Beautiful Arrangement II (Medium)](https://leetcode.com/problems/beautiful-arrangement-ii/description/)
|
||||
|
||||
```html
|
||||
Input: n = 3, k = 2
|
||||
Output: [1, 3, 2]
|
||||
Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.
|
||||
```
|
||||
|
||||
题目描述:数组元素为 1\~n 的整数,要求构建数组,使得相邻元素的差值不相同的个数为 k。
|
||||
|
||||
让前 k+1 个元素构建出 k 个不相同的差值,序列为:1 k+1 2 k 3 k-1 ... k/2 k/2+1.
|
||||
|
||||
```java
|
||||
public int[] constructArray(int n, int k) {
|
||||
int[] ret = new int[n];
|
||||
ret[0] = 1;
|
||||
for (int i = 1, interval = k; i <= k; i++, interval--) {
|
||||
ret[i] = i % 2 == 1 ? ret[i - 1] + interval : ret[i - 1] - interval;
|
||||
}
|
||||
for (int i = k + 1; i < n; i++) {
|
||||
ret[i] = i + 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
# 数组的度
|
||||
|
||||
[697. Degree of an Array (Easy)](https://leetcode.com/problems/degree-of-an-array/description/)
|
||||
|
||||
```html
|
||||
Input: [1,2,2,3,1,4,2]
|
||||
Output: 6
|
||||
```
|
||||
|
||||
题目描述:数组的度定义为元素出现的最高频率,例如上面的数组度为 3。要求找到一个最小的子数组,这个子数组的度和原数组一样。
|
||||
|
||||
```java
|
||||
public int findShortestSubArray(int[] nums) {
|
||||
Map<Integer, Integer> numsCnt = new HashMap<>();
|
||||
Map<Integer, Integer> numsLastIndex = new HashMap<>();
|
||||
Map<Integer, Integer> numsFirstIndex = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
int num = nums[i];
|
||||
numsCnt.put(num, numsCnt.getOrDefault(num, 0) + 1);
|
||||
numsLastIndex.put(num, i);
|
||||
if (!numsFirstIndex.containsKey(num)) {
|
||||
numsFirstIndex.put(num, i);
|
||||
}
|
||||
}
|
||||
int maxCnt = 0;
|
||||
for (int num : nums) {
|
||||
maxCnt = Math.max(maxCnt, numsCnt.get(num));
|
||||
}
|
||||
int ret = nums.length;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
int num = nums[i];
|
||||
int cnt = numsCnt.get(num);
|
||||
if (cnt != maxCnt) continue;
|
||||
ret = Math.min(ret, numsLastIndex.get(num) - numsFirstIndex.get(num) + 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
# 对角元素相等的矩阵
|
||||
|
||||
[766. Toeplitz Matrix (Easy)](https://leetcode.com/problems/toeplitz-matrix/description/)
|
||||
|
||||
```html
|
||||
1234
|
||||
5123
|
||||
9512
|
||||
|
||||
In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", and in each diagonal all elements are the same, so the answer is True.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isToeplitzMatrix(int[][] matrix) {
|
||||
for (int i = 0; i < matrix[0].length; i++) {
|
||||
if (!check(matrix, matrix[0][i], 0, i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < matrix.length; i++) {
|
||||
if (!check(matrix, matrix[i][0], i, 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean check(int[][] matrix, int expectValue, int row, int col) {
|
||||
if (row >= matrix.length || col >= matrix[0].length) {
|
||||
return true;
|
||||
}
|
||||
if (matrix[row][col] != expectValue) {
|
||||
return false;
|
||||
}
|
||||
return check(matrix, expectValue, row + 1, col + 1);
|
||||
}
|
||||
```
|
||||
|
||||
# 嵌套数组
|
||||
|
||||
[565. Array Nesting (Medium)](https://leetcode.com/problems/array-nesting/description/)
|
||||
|
||||
```html
|
||||
Input: A = [5,4,0,3,1,6,2]
|
||||
Output: 4
|
||||
Explanation:
|
||||
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
|
||||
|
||||
One of the longest S[K]:
|
||||
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
|
||||
```
|
||||
|
||||
题目描述:S[i] 表示一个集合,集合的第一个元素是 A[i],第二个元素是 A[A[i]],如此嵌套下去。求最大的 S[i]。
|
||||
|
||||
```java
|
||||
public int arrayNesting(int[] nums) {
|
||||
int max = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
int cnt = 0;
|
||||
for (int j = i; nums[j] != -1; ) {
|
||||
cnt++;
|
||||
int t = nums[j];
|
||||
nums[j] = -1; // 标记该位置已经被访问
|
||||
j = t;
|
||||
|
||||
}
|
||||
max = Math.max(max, cnt);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
# 分隔数组
|
||||
|
||||
[769. Max Chunks To Make Sorted (Medium)](https://leetcode.com/problems/max-chunks-to-make-sorted/description/)
|
||||
|
||||
```html
|
||||
Input: arr = [1,0,2,3,4]
|
||||
Output: 4
|
||||
Explanation:
|
||||
We can split into two chunks, such as [1, 0], [2, 3, 4].
|
||||
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.
|
||||
```
|
||||
|
||||
题目描述:分隔数组,使得对每部分排序后数组就为有序。
|
||||
|
||||
```java
|
||||
public int maxChunksToSorted(int[] arr) {
|
||||
if (arr == null) return 0;
|
||||
int ret = 0;
|
||||
int right = arr[0];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
right = Math.max(right, arr[i]);
|
||||
if (right == i) ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
221
docs/notes/Leetcode 题解 - 栈和队列.md
Normal file
221
docs/notes/Leetcode 题解 - 栈和队列.md
Normal file
|
@ -0,0 +1,221 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [用栈实现队列](#用栈实现队列)
|
||||
* [用队列实现栈](#用队列实现栈)
|
||||
* [最小值栈](#最小值栈)
|
||||
* [用栈实现括号匹配](#用栈实现括号匹配)
|
||||
* [数组中元素与下一个比它大的元素之间的距离](#数组中元素与下一个比它大的元素之间的距离)
|
||||
* [循环数组中比当前元素大的下一个元素](#循环数组中比当前元素大的下一个元素)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 用栈实现队列
|
||||
|
||||
[232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/)
|
||||
|
||||
栈的顺序为后进先出,而队列的顺序为先进先出。使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序。
|
||||
|
||||
```java
|
||||
class MyQueue {
|
||||
|
||||
private Stack<Integer> in = new Stack<>();
|
||||
private Stack<Integer> out = new Stack<>();
|
||||
|
||||
public void push(int x) {
|
||||
in.push(x);
|
||||
}
|
||||
|
||||
public int pop() {
|
||||
in2out();
|
||||
return out.pop();
|
||||
}
|
||||
|
||||
public int peek() {
|
||||
in2out();
|
||||
return out.peek();
|
||||
}
|
||||
|
||||
private void in2out() {
|
||||
if (out.isEmpty()) {
|
||||
while (!in.isEmpty()) {
|
||||
out.push(in.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean empty() {
|
||||
return in.isEmpty() && out.isEmpty();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 用队列实现栈
|
||||
|
||||
[225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/)
|
||||
|
||||
在将一个元素 x 插入队列时,为了维护原来的后进先出顺序,需要让 x 插入队列首部。而队列的默认插入顺序是队列尾部,因此在将 x 插入队列尾部之后,需要让除了 x 之外的所有元素出队列,再入队列。
|
||||
|
||||
```java
|
||||
class MyStack {
|
||||
|
||||
private Queue<Integer> queue;
|
||||
|
||||
public MyStack() {
|
||||
queue = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void push(int x) {
|
||||
queue.add(x);
|
||||
int cnt = queue.size();
|
||||
while (cnt-- > 1) {
|
||||
queue.add(queue.poll());
|
||||
}
|
||||
}
|
||||
|
||||
public int pop() {
|
||||
return queue.remove();
|
||||
}
|
||||
|
||||
public int top() {
|
||||
return queue.peek();
|
||||
}
|
||||
|
||||
public boolean empty() {
|
||||
return queue.isEmpty();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 最小值栈
|
||||
|
||||
[155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/)
|
||||
|
||||
```java
|
||||
class MinStack {
|
||||
|
||||
private Stack<Integer> dataStack;
|
||||
private Stack<Integer> minStack;
|
||||
private int min;
|
||||
|
||||
public MinStack() {
|
||||
dataStack = new Stack<>();
|
||||
minStack = new Stack<>();
|
||||
min = Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
public void push(int x) {
|
||||
dataStack.add(x);
|
||||
min = Math.min(min, x);
|
||||
minStack.add(min);
|
||||
}
|
||||
|
||||
public void pop() {
|
||||
dataStack.pop();
|
||||
minStack.pop();
|
||||
min = minStack.isEmpty() ? Integer.MAX_VALUE : minStack.peek();
|
||||
}
|
||||
|
||||
public int top() {
|
||||
return dataStack.peek();
|
||||
}
|
||||
|
||||
public int getMin() {
|
||||
return minStack.peek();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
对于实现最小值队列问题,可以先将队列使用栈来实现,然后就将问题转换为最小值栈,这个问题出现在 编程之美:3.7。
|
||||
|
||||
# 用栈实现括号匹配
|
||||
|
||||
[20. Valid Parentheses (Easy)](https://leetcode.com/problems/valid-parentheses/description/)
|
||||
|
||||
```html
|
||||
"()[]{}"
|
||||
|
||||
Output : true
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isValid(String s) {
|
||||
Stack<Character> stack = new Stack<>();
|
||||
for (char c : s.toCharArray()) {
|
||||
if (c == '(' || c == '{' || c == '[') {
|
||||
stack.push(c);
|
||||
} else {
|
||||
if (stack.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
char cStack = stack.pop();
|
||||
boolean b1 = c == ')' && cStack != '(';
|
||||
boolean b2 = c == ']' && cStack != '[';
|
||||
boolean b3 = c == '}' && cStack != '{';
|
||||
if (b1 || b2 || b3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
```
|
||||
|
||||
# 数组中元素与下一个比它大的元素之间的距离
|
||||
|
||||
[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/)
|
||||
|
||||
```html
|
||||
Input: [73, 74, 75, 71, 69, 72, 76, 73]
|
||||
Output: [1, 1, 4, 2, 1, 1, 0, 0]
|
||||
```
|
||||
|
||||
在遍历数组时用栈把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
|
||||
|
||||
```java
|
||||
public int[] dailyTemperatures(int[] temperatures) {
|
||||
int n = temperatures.length;
|
||||
int[] dist = new int[n];
|
||||
Stack<Integer> indexs = new Stack<>();
|
||||
for (int curIndex = 0; curIndex < n; curIndex++) {
|
||||
while (!indexs.isEmpty() && temperatures[curIndex] > temperatures[indexs.peek()]) {
|
||||
int preIndex = indexs.pop();
|
||||
dist[preIndex] = curIndex - preIndex;
|
||||
}
|
||||
indexs.add(curIndex);
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
```
|
||||
|
||||
# 循环数组中比当前元素大的下一个元素
|
||||
|
||||
[503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/)
|
||||
|
||||
```text
|
||||
Input: [1,2,1]
|
||||
Output: [2,-1,2]
|
||||
Explanation: The first 1's next greater number is 2;
|
||||
The number 2 can't find next greater number;
|
||||
The second 1's next greater number needs to search circularly, which is also 2.
|
||||
```
|
||||
|
||||
与 739. Daily Temperatures (Medium) 不同的是,数组是循环数组,并且最后要求的不是距离而是下一个元素。
|
||||
|
||||
```java
|
||||
public int[] nextGreaterElements(int[] nums) {
|
||||
int n = nums.length;
|
||||
int[] next = new int[n];
|
||||
Arrays.fill(next, -1);
|
||||
Stack<Integer> pre = new Stack<>();
|
||||
for (int i = 0; i < n * 2; i++) {
|
||||
int num = nums[i % n];
|
||||
while (!pre.isEmpty() && nums[pre.peek()] < num) {
|
||||
next[pre.pop()] = num;
|
||||
}
|
||||
if (i < n){
|
||||
pre.push(i);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
```
|
1121
docs/notes/Leetcode 题解 - 树.md
Normal file
1121
docs/notes/Leetcode 题解 - 树.md
Normal file
File diff suppressed because it is too large
Load Diff
39
docs/notes/Leetcode 题解 - 目录.md
Normal file
39
docs/notes/Leetcode 题解 - 目录.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [算法思想](#算法思想)
|
||||
* [数据结构相关](#数据结构相关)
|
||||
* [参考资料](#参考资料)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
# 算法思想
|
||||
|
||||
- [双指针](Leetcode%20题解%20-%20双指针.md)
|
||||
- [排序](Leetcode%20题解%20-%20排序.md)
|
||||
- [贪心思想](Leetcode%20题解%20-%20贪心思想.md)
|
||||
- [二分查找](Leetcode%20题解%20-%20二分查找.md)
|
||||
- [分治](Leetcode%20题解%20-%20分治.md)
|
||||
- [搜索](Leetcode%20题解%20-%20搜索.md)
|
||||
- [动态规划](Leetcode%20题解%20-%20动态规划.md)
|
||||
- [数学](Leetcode%20题解%20-%20数学.md)
|
||||
|
||||
# 数据结构相关
|
||||
|
||||
- [链表](Leetcode%20题解%20-%20链表.md)
|
||||
- [树](Leetcode%20题解%20-%20树.md)
|
||||
- [栈和队列](Leetcode%20题解%20-%20栈和队列.md)
|
||||
- [哈希表](Leetcode%20题解%20-%20哈希表.md)
|
||||
- [字符串](Leetcode%20题解%20-%20字符串.md)
|
||||
- [数组与矩阵](Leetcode%20题解%20-%20数组与矩阵.md)
|
||||
- [图](Leetcode%20题解%20-%20图.md)
|
||||
- [位运算](Leetcode%20题解%20-%20位运算.md)
|
||||
|
||||
# 参考资料
|
||||
|
||||
|
||||
- Leetcode
|
||||
- Weiss M A, 冯舜玺. 数据结构与算法分析——C 语言描述[J]. 2004.
|
||||
- Sedgewick R. Algorithms[M]. Pearson Education India, 1988.
|
||||
- 何海涛, 软件工程师. 剑指 Offer: 名企面试官精讲典型编程题[M]. 电子工业出版社, 2014.
|
||||
- 《编程之美》小组. 编程之美[M]. 电子工业出版社, 2008.
|
||||
- 左程云. 程序员代码面试指南[M]. 电子工业出版社, 2015.
|
367
docs/notes/Leetcode 题解 - 贪心思想.md
Normal file
367
docs/notes/Leetcode 题解 - 贪心思想.md
Normal file
|
@ -0,0 +1,367 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [分配饼干](#分配饼干)
|
||||
* [不重叠的区间个数](#不重叠的区间个数)
|
||||
* [投飞镖刺破气球](#投飞镖刺破气球)
|
||||
* [根据身高和序号重组队列](#根据身高和序号重组队列)
|
||||
* [分隔字符串使同种字符出现在一起](#分隔字符串使同种字符出现在一起)
|
||||
* [种植花朵](#种植花朵)
|
||||
* [判断是否为子序列](#判断是否为子序列)
|
||||
* [修改一个数成为非递减数组](#修改一个数成为非递减数组)
|
||||
* [股票的最大收益](#股票的最大收益)
|
||||
* [子数组最大的和](#子数组最大的和)
|
||||
* [买入和售出股票最大的收益](#买入和售出股票最大的收益)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。
|
||||
|
||||
# 分配饼干
|
||||
|
||||
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
|
||||
|
||||
```html
|
||||
Input: [1,2], [1,2,3]
|
||||
Output: 2
|
||||
|
||||
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
|
||||
You have 3 cookies and their sizes are big enough to gratify all of the children,
|
||||
You need to output 2.
|
||||
```
|
||||
|
||||
题目描述:每个孩子都有一个满足度,每个饼干都有一个大小,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足。求解最多可以获得满足的孩子数量。
|
||||
|
||||
给一个孩子的饼干应当尽量小又能满足该孩子,这样大饼干就能拿来给满足度比较大的孩子。因为最小的孩子最容易得到满足,所以先满足最小的孩子。
|
||||
|
||||
证明:假设在某次选择中,贪心策略选择给当前满足度最小的孩子分配第 m 个饼干,第 m 个饼干为可以满足该孩子的最小饼干。假设存在一种最优策略,给该孩子分配第 n 个饼干,并且 m < n。我们可以发现,经过这一轮分配,贪心策略分配后剩下的饼干一定有一个比最优策略来得大。因此在后续的分配中,贪心策略一定能满足更多的孩子。也就是说不存在比贪心策略更优的策略,即贪心策略就是最优策略。
|
||||
|
||||
```java
|
||||
public int findContentChildren(int[] g, int[] s) {
|
||||
Arrays.sort(g);
|
||||
Arrays.sort(s);
|
||||
int gi = 0, si = 0;
|
||||
while (gi < g.length && si < s.length) {
|
||||
if (g[gi] <= s[si]) {
|
||||
gi++;
|
||||
}
|
||||
si++;
|
||||
}
|
||||
return gi;
|
||||
}
|
||||
```
|
||||
|
||||
# 不重叠的区间个数
|
||||
|
||||
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
|
||||
|
||||
```html
|
||||
Input: [ [1,2], [1,2], [1,2] ]
|
||||
|
||||
Output: 2
|
||||
|
||||
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
|
||||
```
|
||||
|
||||
```html
|
||||
Input: [ [1,2], [2,3] ]
|
||||
|
||||
Output: 0
|
||||
|
||||
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
|
||||
```
|
||||
|
||||
题目描述:计算让一组区间不重叠所需要移除的区间个数。
|
||||
|
||||
先计算最多能组成的不重叠区间个数,然后用区间总个数减去不重叠区间的个数。
|
||||
|
||||
在每次选择中,区间的结尾最为重要,选择的区间结尾越小,留给后面的区间的空间越大,那么后面能够选择的区间个数也就越大。
|
||||
|
||||
按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。
|
||||
|
||||
```java
|
||||
public int eraseOverlapIntervals(Interval[] intervals) {
|
||||
if (intervals.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
Arrays.sort(intervals, Comparator.comparingInt(o -> o.end));
|
||||
int cnt = 1;
|
||||
int end = intervals[0].end;
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
if (intervals[i].start < end) {
|
||||
continue;
|
||||
}
|
||||
end = intervals[i].end;
|
||||
cnt++;
|
||||
}
|
||||
return intervals.length - cnt;
|
||||
}
|
||||
```
|
||||
|
||||
使用 lambda 表示式创建 Comparator 会导致算法运行时间过长,如果注重运行时间,可以修改为普通创建 Comparator 语句:
|
||||
|
||||
```java
|
||||
Arrays.sort(intervals, new Comparator<Interval>() {
|
||||
@Override
|
||||
public int compare(Interval o1, Interval o2) {
|
||||
return o1.end - o2.end;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
# 投飞镖刺破气球
|
||||
|
||||
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
|
||||
|
||||
```
|
||||
Input:
|
||||
[[10,16], [2,8], [1,6], [7,12]]
|
||||
|
||||
Output:
|
||||
2
|
||||
```
|
||||
|
||||
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
|
||||
|
||||
也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[1, 2] 和 [2, 3] 在本题中算是重叠区间。
|
||||
|
||||
```java
|
||||
public int findMinArrowShots(int[][] points) {
|
||||
if (points.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
Arrays.sort(points, Comparator.comparingInt(o -> o[1]));
|
||||
int cnt = 1, end = points[0][1];
|
||||
for (int i = 1; i < points.length; i++) {
|
||||
if (points[i][0] <= end) {
|
||||
continue;
|
||||
}
|
||||
cnt++;
|
||||
end = points[i][1];
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
```
|
||||
|
||||
# 根据身高和序号重组队列
|
||||
|
||||
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
|
||||
|
||||
Output:
|
||||
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
|
||||
```
|
||||
|
||||
题目描述:一个学生用两个分量 (h, k) 描述,h 表示身高,k 表示排在前面的有 k 个学生的身高比他高或者和他一样高。
|
||||
|
||||
为了使插入操作不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入的第 k 个位置可能会变成第 k+1 个位置。
|
||||
|
||||
身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
|
||||
|
||||
```java
|
||||
public int[][] reconstructQueue(int[][] people) {
|
||||
if (people == null || people.length == 0 || people[0].length == 0) {
|
||||
return new int[0][0];
|
||||
}
|
||||
Arrays.sort(people, (a, b) -> (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
|
||||
List<int[]> queue = new ArrayList<>();
|
||||
for (int[] p : people) {
|
||||
queue.add(p[1], p);
|
||||
}
|
||||
return queue.toArray(new int[queue.size()][]);
|
||||
}
|
||||
```
|
||||
|
||||
# 分隔字符串使同种字符出现在一起
|
||||
|
||||
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
|
||||
|
||||
```html
|
||||
Input: S = "ababcbacadefegdehijhklij"
|
||||
Output: [9,7,8]
|
||||
Explanation:
|
||||
The partition is "ababcbaca", "defegde", "hijhklij".
|
||||
This is a partition so that each letter appears in at most one part.
|
||||
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
|
||||
```
|
||||
|
||||
```java
|
||||
public List<Integer> partitionLabels(String S) {
|
||||
int[] lastIndexsOfChar = new int[26];
|
||||
for (int i = 0; i < S.length(); i++) {
|
||||
lastIndexsOfChar[char2Index(S.charAt(i))] = i;
|
||||
}
|
||||
List<Integer> partitions = new ArrayList<>();
|
||||
int firstIndex = 0;
|
||||
while (firstIndex < S.length()) {
|
||||
int lastIndex = firstIndex;
|
||||
for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
|
||||
int index = lastIndexsOfChar[char2Index(S.charAt(i))];
|
||||
if (index > lastIndex) {
|
||||
lastIndex = index;
|
||||
}
|
||||
}
|
||||
partitions.add(lastIndex - firstIndex + 1);
|
||||
firstIndex = lastIndex + 1;
|
||||
}
|
||||
return partitions;
|
||||
}
|
||||
|
||||
private int char2Index(char c) {
|
||||
return c - 'a';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# 种植花朵
|
||||
|
||||
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
|
||||
|
||||
```html
|
||||
Input: flowerbed = [1,0,0,0,1], n = 1
|
||||
Output: True
|
||||
```
|
||||
|
||||
题目描述:花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
|
||||
|
||||
```java
|
||||
public boolean canPlaceFlowers(int[] flowerbed, int n) {
|
||||
int len = flowerbed.length;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < len && cnt < n; i++) {
|
||||
if (flowerbed[i] == 1) {
|
||||
continue;
|
||||
}
|
||||
int pre = i == 0 ? 0 : flowerbed[i - 1];
|
||||
int next = i == len - 1 ? 0 : flowerbed[i + 1];
|
||||
if (pre == 0 && next == 0) {
|
||||
cnt++;
|
||||
flowerbed[i] = 1;
|
||||
}
|
||||
}
|
||||
return cnt >= n;
|
||||
}
|
||||
```
|
||||
|
||||
# 判断是否为子序列
|
||||
|
||||
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
|
||||
|
||||
```html
|
||||
s = "abc", t = "ahbgdc"
|
||||
Return true.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int index = -1;
|
||||
for (char c : s.toCharArray()) {
|
||||
index = t.indexOf(c, index + 1);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 修改一个数成为非递减数组
|
||||
|
||||
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
|
||||
|
||||
```html
|
||||
Input: [4,2,3]
|
||||
Output: True
|
||||
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
|
||||
```
|
||||
|
||||
题目描述:判断一个数组能不能只修改一个数就成为非递减数组。
|
||||
|
||||
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],只修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]。
|
||||
|
||||
```java
|
||||
public boolean checkPossibility(int[] nums) {
|
||||
int cnt = 0;
|
||||
for (int i = 1; i < nums.length && cnt < 2; i++) {
|
||||
if (nums[i] >= nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
cnt++;
|
||||
if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
|
||||
nums[i] = nums[i - 1];
|
||||
} else {
|
||||
nums[i - 1] = nums[i];
|
||||
}
|
||||
}
|
||||
return cnt <= 1;
|
||||
}
|
||||
```
|
||||
|
||||
# 股票的最大收益
|
||||
|
||||
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
|
||||
|
||||
题目描述:一次股票交易包含买入和卖出,多个交易之间不能交叉进行。
|
||||
|
||||
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,从而在局部最优的情况下也保证全局最优。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int profit = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
if (prices[i] > prices[i - 1]) {
|
||||
profit += (prices[i] - prices[i - 1]);
|
||||
}
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
```
|
||||
|
||||
# 子数组最大的和
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
|
||||
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
# 买入和售出股票最大的收益
|
||||
|
||||
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
|
||||
|
||||
题目描述:只进行一次交易。
|
||||
|
||||
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int n = prices.length;
|
||||
if (n == 0) return 0;
|
||||
int soFarMin = prices[0];
|
||||
int max = 0;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (soFarMin > prices[i]) soFarMin = prices[i];
|
||||
else max = Math.max(max, prices[i] - soFarMin);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
328
docs/notes/Leetcode 题解 - 链表.md
Normal file
328
docs/notes/Leetcode 题解 - 链表.md
Normal file
|
@ -0,0 +1,328 @@
|
|||
* [点击阅读面试进阶指南 ](https://github.com/CyC2018/Backend-Interview-Guide)
|
||||
<!-- GFM-TOC -->
|
||||
* [找出两个链表的交点](#找出两个链表的交点)
|
||||
* [链表反转](#链表反转)
|
||||
* [归并两个有序的链表](#归并两个有序的链表)
|
||||
* [从有序链表中删除重复节点](#从有序链表中删除重复节点)
|
||||
* [删除链表的倒数第 n 个节点](#删除链表的倒数第-n-个节点)
|
||||
* [交换链表中的相邻结点](#交换链表中的相邻结点)
|
||||
* [链表求和](#链表求和)
|
||||
* [回文链表](#回文链表)
|
||||
* [分隔链表](#分隔链表)
|
||||
* [链表元素按奇偶聚集](#链表元素按奇偶聚集)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
链表是空节点,或者有一个值和一个指向下一个链表的指针,因此很多链表问题可以用递归来处理。
|
||||
|
||||
# 找出两个链表的交点
|
||||
|
||||
[160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
|
||||
|
||||
```html
|
||||
A: a1 → a2
|
||||
↘
|
||||
c1 → c2 → c3
|
||||
↗
|
||||
B: b1 → b2 → b3
|
||||
```
|
||||
|
||||
要求:时间复杂度为 O(N),空间复杂度为 O(1)
|
||||
|
||||
设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。
|
||||
|
||||
当访问 A 链表的指针访问到链表尾部时,令它从链表 B 的头部开始访问链表 B;同样地,当访问 B 链表的指针访问到链表尾部时,令它从链表 A 的头部开始访问链表 A。这样就能控制访问 A 和 B 两个链表的指针能同时访问到交点。
|
||||
|
||||
```java
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode l1 = headA, l2 = headB;
|
||||
while (l1 != l2) {
|
||||
l1 = (l1 == null) ? headB : l1.next;
|
||||
l2 = (l2 == null) ? headA : l2.next;
|
||||
}
|
||||
return l1;
|
||||
}
|
||||
```
|
||||
|
||||
如果只是判断是否存在交点,那么就是另一个问题,即 [编程之美 3.6]() 的问题。有两种解法:
|
||||
|
||||
- 把第一个链表的结尾连接到第二个链表的开头,看第二个链表是否存在环;
|
||||
- 或者直接比较两个链表的最后一个节点是否相同。
|
||||
|
||||
# 链表反转
|
||||
|
||||
[206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
|
||||
|
||||
递归
|
||||
|
||||
```java
|
||||
public ListNode reverseList(ListNode head) {
|
||||
if (head == null || head.next == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode next = head.next;
|
||||
ListNode newHead = reverseList(next);
|
||||
next.next = head;
|
||||
head.next = null;
|
||||
return newHead;
|
||||
}
|
||||
```
|
||||
|
||||
头插法
|
||||
|
||||
```java
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode newHead = new ListNode(-1);
|
||||
while (head != null) {
|
||||
ListNode next = head.next;
|
||||
head.next = newHead.next;
|
||||
newHead.next = head;
|
||||
head = next;
|
||||
}
|
||||
return newHead.next;
|
||||
}
|
||||
```
|
||||
|
||||
# 归并两个有序的链表
|
||||
|
||||
[21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
|
||||
|
||||
```java
|
||||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
||||
if (l1 == null) return l2;
|
||||
if (l2 == null) return l1;
|
||||
if (l1.val < l2.val) {
|
||||
l1.next = mergeTwoLists(l1.next, l2);
|
||||
return l1;
|
||||
} else {
|
||||
l2.next = mergeTwoLists(l1, l2.next);
|
||||
return l2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 从有序链表中删除重复节点
|
||||
|
||||
[83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/)
|
||||
|
||||
```html
|
||||
Given 1->1->2, return 1->2.
|
||||
Given 1->1->2->3->3, return 1->2->3.
|
||||
```
|
||||
|
||||
```java
|
||||
public ListNode deleteDuplicates(ListNode head) {
|
||||
if (head == null || head.next == null) return head;
|
||||
head.next = deleteDuplicates(head.next);
|
||||
return head.val == head.next.val ? head.next : head;
|
||||
}
|
||||
```
|
||||
|
||||
# 删除链表的倒数第 n 个节点
|
||||
|
||||
[19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
|
||||
|
||||
```html
|
||||
Given linked list: 1->2->3->4->5, and n = 2.
|
||||
After removing the second node from the end, the linked list becomes 1->2->3->5.
|
||||
```
|
||||
|
||||
```java
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
ListNode fast = head;
|
||||
while (n-- > 0) {
|
||||
fast = fast.next;
|
||||
}
|
||||
if (fast == null) return head.next;
|
||||
ListNode slow = head;
|
||||
while (fast.next != null) {
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
slow.next = slow.next.next;
|
||||
return head;
|
||||
}
|
||||
```
|
||||
|
||||
# 交换链表中的相邻结点
|
||||
|
||||
[24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/)
|
||||
|
||||
```html
|
||||
Given 1->2->3->4, you should return the list as 2->1->4->3.
|
||||
```
|
||||
|
||||
题目要求:不能修改结点的 val 值,O(1) 空间复杂度。
|
||||
|
||||
```java
|
||||
public ListNode swapPairs(ListNode head) {
|
||||
ListNode node = new ListNode(-1);
|
||||
node.next = head;
|
||||
ListNode pre = node;
|
||||
while (pre.next != null && pre.next.next != null) {
|
||||
ListNode l1 = pre.next, l2 = pre.next.next;
|
||||
ListNode next = l2.next;
|
||||
l1.next = next;
|
||||
l2.next = l1;
|
||||
pre.next = l2;
|
||||
|
||||
pre = l1;
|
||||
}
|
||||
return node.next;
|
||||
}
|
||||
```
|
||||
|
||||
# 链表求和
|
||||
|
||||
[445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/)
|
||||
|
||||
```html
|
||||
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
|
||||
Output: 7 -> 8 -> 0 -> 7
|
||||
```
|
||||
|
||||
题目要求:不能修改原始链表。
|
||||
|
||||
```java
|
||||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
|
||||
Stack<Integer> l1Stack = buildStack(l1);
|
||||
Stack<Integer> l2Stack = buildStack(l2);
|
||||
ListNode head = new ListNode(-1);
|
||||
int carry = 0;
|
||||
while (!l1Stack.isEmpty() || !l2Stack.isEmpty() || carry != 0) {
|
||||
int x = l1Stack.isEmpty() ? 0 : l1Stack.pop();
|
||||
int y = l2Stack.isEmpty() ? 0 : l2Stack.pop();
|
||||
int sum = x + y + carry;
|
||||
ListNode node = new ListNode(sum % 10);
|
||||
node.next = head.next;
|
||||
head.next = node;
|
||||
carry = sum / 10;
|
||||
}
|
||||
return head.next;
|
||||
}
|
||||
|
||||
private Stack<Integer> buildStack(ListNode l) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
while (l != null) {
|
||||
stack.push(l.val);
|
||||
l = l.next;
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
```
|
||||
|
||||
# 回文链表
|
||||
|
||||
[234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
|
||||
|
||||
题目要求:以 O(1) 的空间复杂度来求解。
|
||||
|
||||
切成两半,把后半段反转,然后比较两半是否相等。
|
||||
|
||||
```java
|
||||
public boolean isPalindrome(ListNode head) {
|
||||
if (head == null || head.next == null) return true;
|
||||
ListNode slow = head, fast = head.next;
|
||||
while (fast != null && fast.next != null) {
|
||||
slow = slow.next;
|
||||
fast = fast.next.next;
|
||||
}
|
||||
if (fast != null) slow = slow.next; // 偶数节点,让 slow 指向下一个节点
|
||||
cut(head, slow); // 切成两个链表
|
||||
return isEqual(head, reverse(slow));
|
||||
}
|
||||
|
||||
private void cut(ListNode head, ListNode cutNode) {
|
||||
while (head.next != cutNode) {
|
||||
head = head.next;
|
||||
}
|
||||
head.next = null;
|
||||
}
|
||||
|
||||
private ListNode reverse(ListNode head) {
|
||||
ListNode newHead = null;
|
||||
while (head != null) {
|
||||
ListNode nextNode = head.next;
|
||||
head.next = newHead;
|
||||
newHead = head;
|
||||
head = nextNode;
|
||||
}
|
||||
return newHead;
|
||||
}
|
||||
|
||||
private boolean isEqual(ListNode l1, ListNode l2) {
|
||||
while (l1 != null && l2 != null) {
|
||||
if (l1.val != l2.val) return false;
|
||||
l1 = l1.next;
|
||||
l2 = l2.next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 分隔链表
|
||||
|
||||
[725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
|
||||
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
|
||||
Explanation:
|
||||
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
|
||||
```
|
||||
|
||||
题目描述:把链表分隔成 k 部分,每部分的长度都应该尽可能相同,排在前面的长度应该大于等于后面的。
|
||||
|
||||
```java
|
||||
public ListNode[] splitListToParts(ListNode root, int k) {
|
||||
int N = 0;
|
||||
ListNode cur = root;
|
||||
while (cur != null) {
|
||||
N++;
|
||||
cur = cur.next;
|
||||
}
|
||||
int mod = N % k;
|
||||
int size = N / k;
|
||||
ListNode[] ret = new ListNode[k];
|
||||
cur = root;
|
||||
for (int i = 0; cur != null && i < k; i++) {
|
||||
ret[i] = cur;
|
||||
int curSize = size + (mod-- > 0 ? 1 : 0);
|
||||
for (int j = 0; j < curSize - 1; j++) {
|
||||
cur = cur.next;
|
||||
}
|
||||
ListNode next = cur.next;
|
||||
cur.next = null;
|
||||
cur = next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
# 链表元素按奇偶聚集
|
||||
|
||||
[328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/)
|
||||
|
||||
```html
|
||||
Example:
|
||||
Given 1->2->3->4->5->NULL,
|
||||
return 1->3->5->2->4->NULL.
|
||||
```
|
||||
|
||||
```java
|
||||
public ListNode oddEvenList(ListNode head) {
|
||||
if (head == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode odd = head, even = head.next, evenHead = even;
|
||||
while (even != null && even.next != null) {
|
||||
odd.next = odd.next.next;
|
||||
odd = odd.next;
|
||||
even.next = even.next.next;
|
||||
even = even.next;
|
||||
}
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
}
|
||||
```
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user