CS-Notes/notes/Leetcode 题解.md

4895 lines
131 KiB
Markdown
Raw Normal View History

2018-02-13 23:29:52 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:54 +08:00
* [<EFBFBD>㷨˼<EFBFBD><EFBFBD>](#<23>㷨˼<E3B7A8><CBBC>)
* [<EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>)
* [̰<EFBFBD><EFBFBD>˼<EFBFBD><EFBFBD>](<><CCB0>˼<EFBFBD><CBBC>)
* [˫ָ<EFBFBD><EFBFBD>](#˫ָ<CBAB><D6B8>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [Ͱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](<><CDB0><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
2018-02-13 23:29:52 +08:00
* [BFS](#bfs)
* [DFS](#dfs)
* [Backtracking](#backtracking)
2018-02-22 14:47:54 +08:00
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD>ݹ<EFBFBD>](#<23>ݹ<EFBFBD>)
* [<EFBFBD><EFBFBD>̬<EFBFBD>](#<23><>̬<EFBFBD>)
* [<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#쳲<><ECB3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD>](#<23><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>)
* [0-1 <20><><EFBFBD><EFBFBD>](#0-1-<2D><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD>ѧ](#<23><>ѧ)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>)
* [<EFBFBD>׳<EFBFBD>](#<23>׳<EFBFBD>)
* [<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͶƱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>ͶƱ<CDB6><C6B1><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD>ݽṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD>ݽṹ<DDBD><E1B9B9><EFBFBD><EFBFBD>)
* [ջ<EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>](<>Ͷ<EFBFBD><CDB6><EFBFBD>)
* [<EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD>](#<23><>ϣ<EFBFBD><CFA3>)
* [<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>](#<23>ַ<EFBFBD><D6B7><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD>](#<23><>)
* [<EFBFBD>ݹ<EFBFBD>](#<23>ݹ<EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD>α<EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD>α<EFBFBD><CEB1><EFBFBD>)
* [ǰ<EFBFBD>к<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](<>к<EFBFBD><D0BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
2018-02-13 23:29:52 +08:00
* [BST](#bst)
* [Trie](#trie)
2018-02-22 14:47:54 +08:00
* [ͼ]()
* [λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](<><CEBB><EFBFBD><EFBFBD>)
* [<EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>)
2018-02-13 23:29:52 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:54 +08:00
# <20>㷨˼<E3B7A8><CBBC>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD><EFBFBD>˼<EFBFBD><EFBFBD><EFBFBD>򵥣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD>Ҫע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD>ڣ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
1. <20>ڼ<EFBFBD><DABC><EFBFBD> mid ʱ<><CAB1><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> mid = (l + h) / 2 <20><><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>Ϊ l + h <20><><EFBFBD>ܻᵼ<DCBB>¼ӷ<C2BC><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ʹ<EFBFBD><CAB9> mid = l + (h - l) / 2 <20><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
2. <20><> h <20>ĸ<EFBFBD>ֵ<EFBFBD><D6B5>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>йأ<D0B9><D8A3><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ l <= h ʱ<><CAB1>h = mid - 1<><31><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ l < h ʱ<EFBFBD><EFBFBD>h = mid<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ l <= h ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> h = mid<69><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>޷<EFBFBD><DEB7>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> l = 1<><31>h = 1<><31><EFBFBD><EFBFBD>ʱ mid Ҳ<><D2B2><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4> h = mid <20><><EFBFBD><EFBFBD>ô<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ l < h <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> h = mid - 1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1,2,3 <EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <EFBFBD><EFBFBD><EFBFBD>ʼ l = 0<EFBFBD><EFBFBD>h = 2<EFBFBD><EFBFBD>mid = 1<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> key < arr[mid] ִ<EFBFBD><EFBFBD> h = mid - 1 = 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱѭ<EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӰѲ<EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
3. l <20>ĸ<EFBFBD>ֵһ<D6B5>㶼Ϊ l = mid + 1<><31>
2018-02-13 23:29:52 +08:00
```java
public int search(int key, int[] arr) {
int l = 0, h = arr.length - 1;
while (l <= h) {
int mid = l + (h - l) / 2;
if (key == arr[mid]) return mid;
if (key < arr[mid]) h = mid - 1;
else l = mid + 1;
}
return -1;
}
```
2018-02-22 14:47:54 +08:00
**<2A>󿪷<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/)
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> x <20>Ŀ<EFBFBD><C4BF><EFBFBD> sqrt һ<><D2BB><EFBFBD><EFBFBD> 0 \~ x ֮<><EFBFBD><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sqrt == x / sqrt <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD> 0 \~ x ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sqrt<72><74>
2018-02-13 23:29:52 +08:00
```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(sqrt < mid) h = mid - 1;
else l = mid + 1;
}
return h;
}
```
2018-02-22 14:47:54 +08:00
**<2A><>Ӳ<EFBFBD><D3B2>**
2018-02-13 23:29:52 +08:00
[Leetcode : 441. Arranging Coins (Easy)](https://leetcode.com/problems/arranging-coins/description/)
```html
n = 8
The coins can form the following rows:
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD> <20><>
<EFBFBD><EFBFBD> <20><> <20><>
<EFBFBD><EFBFBD> <20><>
2018-02-13 23:29:52 +08:00
Because the 4th row is incomplete, we return 3.
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20>а<EFBFBD> i <20><><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> h <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> l<><6C><EFBFBD><EFBFBD>Ϊ<EFBFBD>ڵ<EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5>
2018-02-13 23:29:52 +08:00
```java
public int arrangeCoins(int n) {
int l = 0, h = n;
while(l <= h){
int m = l + (h - l) / 2;
long x = m * (m + 1L) / 2;
if(x == n) return m;
else if(x < n) l = m + 1;
else h = m - 1;
}
return h;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD>ң<EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>۵Ľⷨ<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int arrangeCoins(int n) {
int level = 1;
while (n > 0) {
n -= level;
level++;
}
return n == 0 ? level - 1 : level - 2;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Single Element**
2018-02-13 23:29:52 +08:00
[Leetcode : 540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int singleNonDuplicate(int[] nums) {
int l = 0, h = nums.length - 1;
while(l < h) {
int m = l + (h - l) / 2;
2018-02-22 14:47:54 +08:00
if(m % 2 == 1) m--; // <20><>֤ l/h/m <20><><EFBFBD><EFBFBD>ż<EFBFBD><C5BC>λ<EFBFBD><CEBB>ʹ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сһֱ<D2BB><D6B1><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
if(nums[m] == nums[m + 1]) l = m + 2;
else h = m;
}
return nums[l];
}
```
2018-02-22 14:47:54 +08:00
## ̰<><CCB0>˼<EFBFBD><CBBC>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
̰<EFBFBD><EFBFBD>˼<EFBFBD>뱣֤ÿ<EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǿֲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵġ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>ֻ<EFBFBD>б<EFBFBD><EFBFBD>ɵĴ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD>ӲŻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>С<EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵı<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱȽϴ<EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD>ӡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>̰<EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD> m <20><><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD>ҵ<EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD> m <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>С<EFBFBD><D0A1><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>տ<EFBFBD><D5BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ų<EFBFBD><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>и<EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD> n <20><><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɴ<EFBFBD><C9B4>ڵ<EFBFBD> m <20><><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>Ų<EFBFBD><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ʹ<EFBFBD>õ<EFBFBD> m <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> n <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD>IJ<EFBFBD><C4B2>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ų<EFBFBD><C5B2>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵġ<C5B5>
2018-02-13 23:29:52 +08:00
```java
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0;
while(i < g.length && j < s.length){
if(g[i] <= s[j]) i++;
j++;
}
return i;
}
```
2018-02-22 14:47:54 +08:00
**Ͷ<><CDB6><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ˮƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϰڷţ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>򶼻<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int findMinArrowShots(int[][] points) {
if(points.length == 0) return 0;
Arrays.sort(points,(a,b) -> (a[1] - b[1]));
int curPos = points[0][1];
int ret = 1;
for (int i = 1; i < points.length; i++) {
if(points[i][0] <= curPos) {
continue;
}
curPos = points[i][1];
ret++;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A><>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۹<EFBFBD>Ʊ<EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD>۴<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [a, b, c, d]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> a <= b <= c <= d <20><><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ d - a = (d - c) + (c - b) + (b - a) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>һ<EFBFBD><D2BB> prices[i] <20><> prices[i] - prices[i-1] <20><><EFBFBD><EFBFBD>ô<EFBFBD>Ͱ<EFBFBD> prices[i] - prices[i-1] <20>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3>Ӷ<EFBFBD><D3B6>ھֲ<DABE><D6B2><EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>֤ȫ<D6A4><C8AB><EFBFBD><EFBFBD><EFBFBD>š<EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
**<2A><>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int cnt = 0;
for(int i = 0; i < flowerbed.length; i++){
if(flowerbed[i] == 1) continue;
int pre = i == 0 ? 0 : flowerbed[i - 1];
int next = i == flowerbed.length - 1 ? 0 : flowerbed[i + 1];
if(pre == 0 && next == 0) {
cnt++;
flowerbed[i] = 1;
}
}
return cnt >= n;
}
```
2018-02-22 14:47:54 +08:00
**<2A>޸<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ǵݼ<C7B5><DDBC><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܲ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>޸<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>Ϊ<EFBFBD>ǵݼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> nums[i] < nums[i - 1] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums[i - 1] = nums[i]<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD> nums[i] = nums[i - 1] <EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô nums[i] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><EFBFBD>ܱ<EFBFBD> nums[i + 1] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ nums[i] <EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums[i] < nums[i - 2] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>ֻ<EFBFBD>޸<EFBFBD> nums[i - 1] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ǵݼ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD> nums[i] = nums[i - 1] <EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean checkPossibility(int[] nums) {
int cnt = 0;
for(int i = 1; i < nums.length; i++){
if(nums[i] < nums[i - 1]){
cnt++;
if(i - 2 >= 0 && nums[i - 2] > nums[i]) nums[i] = nums[i-1];
else nums[i - 1] = nums[i];
}
}
return cnt <= 1;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD>Ӵ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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) {
for (int i = 0, pos = 0; i < s.length(); i++, pos++) {
pos = t.indexOf(s.charAt(i), pos);
if(pos == -1) return false;
}
return true;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ָ<EFBFBD><D6B8>ַ<EFBFBD><D6B7><EFBFBD>ʹͬ<CAB9><CDAC><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>**
2018-02-13 23:29:52 +08:00
[Leetcode : 763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
```java
public List<Integer> partitionLabels(String S) {
List<Integer> ret = new ArrayList<>();
int[] lastIdxs = new int[26];
for(int i = 0; i < S.length(); i++) lastIdxs[S.charAt(i) - 'a'] = i;
int startIdx = 0;
while(startIdx < S.length()) {
int endIdx = startIdx;
for(int i = startIdx; i < S.length() && i <= endIdx; i++) {
int lastIdx = lastIdxs[S.charAt(i) - 'a'];
if(lastIdx == i) continue;
if(lastIdx > endIdx) endIdx = lastIdx;
}
ret.add(endIdx - startIdx + 1);
startIdx = endIdx + 1;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߺ<EFBFBD><DFBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
```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]]
```
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD>ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (h, k) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>h <20><>ʾ<EFBFBD><CABE><EFBFBD>ߣ<EFBFBD>k <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> k <20><>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD>߻<EFBFBD><DFBB>ߺ<EFBFBD><DFBA><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ߡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>k ֵ<><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĵ<D0B5> k <20><>λ<EFBFBD><CEBB><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int[][] reconstructQueue(int[][] people) {
if(people == null || people.length == 0 || people[0].length == 0) return new int[0][0];
Arrays.sort(people, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0] == b[0]) return a[1] - b[1];
return b[0] - a[0];
}
});
int n = people.length;
List<int[]> tmp = new ArrayList<>();
for(int i = 0; i < n; i++) {
tmp.add(people[i][1], new int[]{people[i][0], people[i][1]});
}
int[][] ret = new int[n][2];
for(int i = 0; i < n; i++) {
ret[i][0] = tmp.get(i)[0];
ret[i][1] = tmp.get(i)[1];
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
## ˫ָ<CBAB><D6B8>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
˫ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Эͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>һ<EFBFBD><D2BB><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>ǵĺ<C7B5>Ϊ 0**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[Leetcode <20><>167. Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD>˫ָ<EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD>С<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽϴ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>СԪ<EFBFBD>ص<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ԫ<EFBFBD>صĺ<EFBFBD> sum == target<65><74><EFBFBD><EFBFBD>ô<EFBFBD>õ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sum > target<65><74><EFBFBD>ƶ<EFBFBD><C6B6>ϴ<EFBFBD><CFB4><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>ʹ sum <20><>СһЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sum < target<EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>ʹ sum <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>غ<EFBFBD>Ϊ 0 <20><><EFBFBD><EFBFBD><EFBFBD>⣺[<5B><><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> P351](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۼӺ<DBBC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> P354/P355/P358](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD>к<EFBFBD><D0BA>ж<EFBFBD><D0B6>ź<EFBFBD><C5BA><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>Ԫ<EFBFBD><D4AA>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> P347](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>ַ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD>˫ָ<EFBFBD>룬ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private HashSet<Character> vowels = new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
public String reverseVowels(String s) {
if(s.length() == 0) return 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;
i++;
} else if(!vowels.contains(cj)){
result[j] = cj;
j--;
} else{
result[i] = cj;
result[j] = ci;
i++;
j--;
}
}
return new String(result);
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>ж<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 5 = 1<sup>2</sup> + 2<sup>2</sup><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean judgeSquareSum(int c) {
int left = 0, right = (int) Math.sqrt(c);
while(left <= right){
int powSum = left * left + right * right;
if(powSum == c) return true;
else if(powSum > c) right--;
else left++;
}
return false;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/)
```java
public boolean validPalindrome(String s) {
int i = 0, j = s.length() -1;
while(i < j){
if(s.charAt(i) != s.charAt(j)){
return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
}
i++;
j--;
}
return true;
}
private boolean isPalindrome(String s, int l, int r){
while(l < r){
if(s.charAt(l) != s.charAt(r))
return false;
l++;
r--;
}
return true;
}
```
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD><E9B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>ѹ鲢<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public void merge(int[] nums1, int m, int[] nums2, int n) {
2018-02-22 14:47:54 +08:00
int i = m - 1, j = n - 1; // <20><>Ҫ<EFBFBD><D2AA>β<EFBFBD><CEB2>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums1 <20>Ϲ<CFB9>õ<EFBFBD><C3B5><EFBFBD>ֵ<EFBFBD><EFBFBD>ǻ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD>й鲢<D0B9>Ƚϵ<C8BD>ֵ
2018-02-13 23:29:52 +08:00
int idx = m + n - 1;
while(i >= 0 || j >= 0){
if(i < 0) nums1[idx] = nums2[j--];
else if(j < 0) nums1[idx] = nums1[i--];
else if(nums1[i] > nums2[j]) nums1[idx] = nums1[i--];
else nums1[idx] = nums2[j--];
idx--;
}
}
```
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڻ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/)
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD>˫ָ<EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ڵ㣬һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean hasCycle(ListNode head) {
if(head == null) return false;
ListNode l1 = head, l2 = head.next;
while(l1 != null && l2 != null){
if(l1 == l2) return true;
l1 = l1.next;
if(l2.next == null) break;
l2 = l2.next.next;
}
return false;
}
```
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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"
```
```java
public String findLongestWord(String s, List<String> d) {
String ret = "";
for (String str : d) {
for (int i = 0, j = 0; i < s.length() && j < str.length(); i++) {
if (s.charAt(i) == str.charAt(j)) j++;
if (j == str.length()) {
if (ret.length() < str.length()
|| (ret.length() == str.length() && ret.compareTo(str) > 0)) {
ret = str;
}
}
}
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> **Kth Element** <20><><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(n) ʱ<><EFBFBD>Ӷȣ<D3B6>O(1) <20>ռ<D5BC>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2B9A4><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(n<sup>2</sup>)<29><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> **TopK Elements** <20><><EFBFBD>⣬ͨ<E2A3AC><CDA8>ά<EFBFBD><CEAC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>СΪ K <20>Ķѣ<C4B6><D1A3><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD> TopK Elements<74><73><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Kth Element <20><><EFBFBD><EFBFBD><E2A3AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD><D1B5>Ǹ<EFBFBD>Ԫ<EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD> Kth Element<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TopK Elements <20><><EFBFBD><EFBFBD><E2A3AC>Ϊ<EFBFBD>ҵ<EFBFBD> Kth Element ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD>ٱ<EFBFBD><D9B1><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD>С<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD> Kth Element <20><>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD> TopK Elements<74><73><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD><EFBFBD>򶼿<EFBFBD><F2B6BCBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Kth Element <20><> TopK Elements <20><><EFBFBD>⣬ֻ<E2A3AC><D6BB><EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
**Kth Element**
[Leetocde : 215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD>[Solution explained](https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
- ʱ<><EFBFBD>Ӷ<EFBFBD> O(nlgn)<29><><EFBFBD>ռ<D5BC>Ӷ<EFBFBD> O(1) <20><EFBFBD><E2B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
- ʱ<><EFBFBD>Ӷ<EFBFBD> O(nlgk)<29><><EFBFBD>ռ<D5BC>Ӷ<EFBFBD> O(k) <20><EFBFBD><E2B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
- ʱ<><EFBFBD>Ӷ<EFBFBD> O(n)<29><><EFBFBD>ռ<D5BC>Ӷ<EFBFBD> O(1) <20><EFBFBD><E2B7A8>QuickSelect
2018-02-13 23:29:52 +08:00
**ToK Elements**
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> P336](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### Ͱ<><CDB0><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/)
2018-02-22 14:47:54 +08:00
Ͱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public List<Integer> topKFrequent(int[] nums, int k) {
List<Integer> ret = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for(int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
List<Integer>[] bucket = new List[nums.length + 1];
for(int key : map.keySet()) {
int frequency = map.get(key);
if(bucket[frequency] == null) {
bucket[frequency] = new ArrayList<>();
}
bucket[frequency].add(key);
}
for(int i = bucket.length - 1; i >= 0 && ret.size() < k; i--) {
if(bucket[i] != null) {
ret.addAll(bucket[i]);
}
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>Ӧ<EFBFBD><EFBFBD>ԶԶ<EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD>
2018-02-13 23:29:52 +08:00
### BFS
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4ff355cf-9a7f-4468-af43-e5b02038facc.jpg)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<EFBFBD> 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6<><36>2<EFBFBD><32>1 <20><> 5 <20><><EFBFBD>ĸ<EFBFBD><C4B8>½ڵ㡣
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>ڵ<EFBFBD> 4 <20><><EFBFBD><EFBFBD> 2 <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ㣻<DAB5><E3A3BB> 1 <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ㣻<DAB5><E3A3BB> 5 <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> 3 <20><> 4 <20>ڵ㡣<DAB5><E3A1A3>һ<EFBFBD><D2BB><EFBFBD>ܹ<EFBFBD><DCB9>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ڵ㣺4 <20><> 3 <20><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿһ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ㶼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> d<sub>i</sub> <20><>ʾ<EFBFBD><CABE> i <20><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3>Ƶ<EFBFBD><C6B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ۣ<EFBFBD><DBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD>Ľڵ<C4BD> i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD> j<><6A><EFBFBD><EFBFBD> d<sub>i</sub><=d<sub>j</sub><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD> **<EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD>** <20><><EFBFBD><EFBFBD><E2A3BA>һ<EFBFBD>α<EFBFBD><CEB1><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>Ľڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ٱ<EFBFBD><D9B1><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>Ľڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>ڳ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD> BFS ʱ<><CAB1>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
- <20><><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢ÿһ<C3BF>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>Ľڵ<C4BD>
- <20><><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ýڵ㣬Ӧ<E3A3AC>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD>ֹ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>ԭ<EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
```html
[[1,1,0,1],
[1,0,1,0],
[1,1,1,1],
[1,0,1,1]]
```
```java
public int minPathLength(int[][] grids, int tr, int tc) {
int[][] next = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m = grids.length, n = grids[0].length;
Queue<Position> queue = new LinkedList<>();
queue.add(new Position(0, 0, 1));
while (!queue.isEmpty()) {
Position pos = queue.poll();
for (int i = 0; i < 4; i++) {
Position nextPos = new Position(pos.r + next[i][0], pos.c + next[i][1], pos.length + 1);
if (nextPos.r < 0 || nextPos.r >= m || nextPos.c < 0 || nextPos.c >= n) continue;
if (grids[nextPos.r][nextPos.c] != 1) continue;
grids[nextPos.r][nextPos.c] = 0;
if (nextPos.r == tr && nextPos.c == tc) return nextPos.length;
queue.add(nextPos);
}
}
return -1;
}
private class Position {
int r, c, length;
public Position(int r, int c, int length) {
this.r = r;
this.c = c;
this.length = length;
}
}
```
### DFS
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7f7e3e5-7dd4-4173-9999-576b9e2ac0a2.png)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ڵ㣬Ҫ<EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ洢<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ٱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>½ڵ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<EFBFBD> 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>½ڵ<C2BD> 6 ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ڵ<C2BD> 6 <20><><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>½ڵ<C2BD> 4<><34><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>½ڵ㣬ֱ<E3A3AC><D6B1>û<EFBFBD><C3BB><EFBFBD>½ڵ<C2BD><DAB5>ˣ<EFBFBD><CBA3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ء<EFBFBD><D8A1><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>ڵ<EFBFBD> 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8>ڵ<EFBFBD> 0 <20><><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>½ڵ<C2BD> 2<><32>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><CFB2>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> DFS <20><>һ<EFBFBD><D2BB>ͼ<EFBFBD><CDBC><EFBFBD>б<EFBFBD><D0B1><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ㶼<DAB5>Ǵӳ<C7B4>ʼ<EFBFBD>ڵ<EFBFBD><DAB5>ɴ<EFBFBD><C9B4>ģ<EFBFBD>DFS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> **<EFBFBD>ɴ<EFBFBD><EFBFBD><EFBFBD>** <20><><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>ڳ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD> DFS ʱ<><CAB1>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
- ջ<><D5BB><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>浱ǰ<E6B5B1>ڵ<EFBFBD><DAB5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ڵ㷵<DAB5><E3B7B5>ʱ<EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ڵ㡣Ҳ<E3A1A3><D2B2><EFBFBD><EFBFBD>ʹ<EFBFBD>õݹ<C3B5>ջ<EFBFBD><D5BB>
- <20><><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD> BFS һ<><D2BB>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ýڵ<C3BD><DAB5><EFBFBD><EFBFBD>б<EFBFBD><D0B1>ǡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 695. Max Area of Island (Easy)](https://leetcode.com/problems/max-area-of-island/description/)
```html
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
```
```java
public int maxAreaOfIsland(int[][] grid) {
int m = grid.length, n = grid[0].length;
int max = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 1) max = Math.max(max, dfs(grid, i, j));
}
}
return max;
}
private int dfs(int[][] grid, int i, int j){
int m = grid.length, n = grid[0].length;
if(i < 0 || i >= m || j < 0 || j >= n) return 0;
if(grid[i][j] == 0) return 0;
grid[i][j] = 0;
return dfs(grid, i + 1, j) + dfs(grid, i - 1, j) + dfs(grid, i, j + 1) + dfs(grid, i, j - 1) + 1;
}
```
2018-02-22 14:47:54 +08:00
**ͼ<><CDBC><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 547. Friend Circles (Medium)](https://leetcode.com/problems/friend-circles/description/)
```html
Input:
[[1,1,0],
[1,1,0],
[0,0,1]]
Output: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
The 2nd student himself is in a friend circle. So return 2.
```
```java
public int findCircleNum(int[][] M) {
int n = M.length;
int ret = 0;
boolean[] hasFind = new boolean[n];
for(int i = 0; i < n; i++) {
if(!hasFind[i]) {
dfs(M, i, hasFind);
ret++;
}
}
return ret;
}
private void dfs(int[][] M, int i, boolean[] hasFind) {
hasFind[i] = true;
int n = M.length;
for(int k = 0; k < n; k++) {
if(M[i][k] == 1 && !hasFind[k]) {
dfs(M, k, hasFind);
}
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 200. Number of Islands (Medium)](https://leetcode.com/problems/number-of-islands/description/)
```html
11110
11010
11000
00000
Answer: 1
```
```java
private int m, n;
private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) return 0;
m = grid.length;
n = grid[0].length;
int ret = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
dfs(grid, i, j);
ret++;
}
}
}
return ret;
}
private void dfs(char[][] grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') return;
grid[i][j] = '0';
for (int k = 0; k < direction.length; k++) {
dfs(grid, i + direction[k][0], j + direction[k][1]);
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>дӸ<D0B4><D3B8><EFBFBD>Ҷ<EFBFBD>ӵ<EFBFBD>·<EFBFBD><C2B7>**
2018-02-13 23:29:52 +08:00
[Leetcode : 257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths/description/)
```html
1
/ \
2 3
\
5
```
```html
["1->2->5", "1->3"]
```
```java
public List<String> binaryTreePaths(TreeNode root) {
List<String> ret = new ArrayList();
if(root == null) return ret;
dfs(root, "", ret);
return ret;
}
private void dfs(TreeNode root, String prefix, List<String> ret){
if(root == null) return;
if(root.left == null && root.right == null){
ret.add(prefix + root.val);
return;
}
prefix += (root.val + "->");
dfs(root.left, prefix, ret);
dfs(root.right, prefix, ret);
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 130. Surrounded Regions (Medium)](https://leetcode.com/problems/surrounded-regions/description/)
```html
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ñ<EFBFBD> 'X' <20><> 'O' ת<><D7AA>Ϊ 'X'<27><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬ʣ<EFBFBD>µľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
private int m, n;
public void solve(char[][] board) {
if (board == null || board.length == 0) return;
m = board.length;
n = board[0].length;
for (int i = 0; i < m; i++) {
dfs(board, i, 0);
dfs(board, i, n - 1);
}
for (int i = 0; i < n; i++) {
dfs(board, 0, i);
dfs(board, m - 1, i);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'T') board[i][j] = 'O';
else if (board[i][j] == 'O') board[i][j] = 'X';
}
}
}
private void dfs(char[][] board, int r, int c) {
if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] != 'O') return;
board[r][c] = 'T';
for (int i = 0; i < direction.length; i++) {
dfs(board, r + direction[i][0], c + direction[i][1]);
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/description/)
```html
Given the following 5x5 matrix:
Pacific \~ \~ \~ \~ \~
\~ 1 2 2 3 (5) *
\~ 3 2 3 (4) (4) *
\~ 2 4 (5) 3 1 *
\~ (6) (7) 1 4 5 *
\~ (5) 1 1 2 4 *
* * * * * Atlantic
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߺ<EFBFBD><EFBFBD>ϱ<EFBFBD><EFBFBD><EFBFBD>̫ƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ұߺ<EFBFBD><EFBFBD>±<EFBFBD><EFBFBD>Ǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>θߵĵط<EFBFBD><EFBFBD><EFBFBD>ˮ<EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ĵط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮ<EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̫ƽ<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еط<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private int m, n;
private int[][] matrix;
private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int[]> ret = new ArrayList<>();
if (matrix == null || matrix.length == 0) return ret;
this.m = matrix.length;
this.n = matrix[0].length;
this.matrix = matrix;
boolean[][] canReachP = new boolean[m][n];
boolean[][] canReachA = new boolean[m][n];
for (int i = 0; i < m; i++) {
dfs(i, 0, canReachP);
dfs(i, n - 1, canReachA);
}
for (int i = 0; i < n; i++) {
dfs(0, i, canReachP);
dfs(m - 1, i, canReachA);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (canReachP[i][j] && canReachA[i][j]) {
ret.add(new int[]{i, j});
}
}
}
return ret;
}
private void dfs(int r, int c, boolean[][] canReach) {
if(canReach[r][c]) return;
canReach[r][c] = true;
for (int i = 0; i < direction.length; i++) {
int nextR = direction[i][0] + r;
int nextC = direction[i][1] + c;
if (nextR < 0 || nextR >= m || nextC < 0 || nextC >= n
|| matrix[r][c] > matrix[nextR][nextC]) continue;
dfs(nextR, nextC, canReach);
}
}
```
2018-02-22 14:47:54 +08:00
**N <20>ʺ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 51. N-Queens (Hard)](https://leetcode.com/problems/n-queens/description/)
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1f080e53-4758-406c-bb5f-dbedf89b63ce.jpg)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> n\*n <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD>аڷ<D0B0> n <20><><EFBFBD>ʺ󣬲<CABA><F3A3ACB2><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ʺ<EFBFBD><CABA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬһ<CDAC>У<EFBFBD>ͬһ<CDAC>У<EFBFBD>ͬһ<CDAC>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD>ϣ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> n <20>ʺ<EFBFBD><CABA>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD>һ<EFBFBD>еذڷţ<EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>Ǹ<EFBFBD><EFBFBD>ʺ<EFBFBD>Ӧ<EFBFBD>ð<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>ijһ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>顢45 <20>ȶԽ<C8B6><D4BD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 135 <20>ȶԽ<C8B6><D4BD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
45 <20>ȶԽ<C8B6><D4BD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>Ϊ 2\*n - 1<><31>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ (r,c) <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ϊ r + c<><63>
2018-02-13 23:29:52 +08:00
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/85583359-1b45-45f2-9811-4f7bb9a64db7.jpg)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
135 <20>ȶԽ<C8B6><D4BD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>Ҳ<EFBFBD><D2B2> 2\*n - 1<><31>(r,c) <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ϊ n - 1 - (r - c)<29><>
2018-02-13 23:29:52 +08:00
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9e80f75a-b12b-4344-80c8-1f9ccc2d5246.jpg)
2018-02-13 23:29:52 +08:00
```java
private List<List<String>> ret;
private char[][] nQueens;
private boolean[] colUsed;
private boolean[] diagonals45Used;
private boolean[] diagonals135Used;
private int n;
public List<List<String>> solveNQueens(int n) {
ret = new ArrayList<>();
nQueens = new char[n][n];
Arrays.fill(nQueens, '.');
colUsed = new boolean[n];
diagonals45Used = new boolean[2 * n - 1];
diagonals135Used = new boolean[2 * n - 1];
this.n = n;
backstracking(0);
return ret;
}
private void backstracking(int row) {
if (row == n) {
List<String> list = new ArrayList<>();
for (char[] chars : nQueens) {
list.add(new String(chars));
}
ret.add(list);
return;
}
for (int col = 0; col < n; col++) {
int diagonals45Idx = row + col;
int diagonals135Idx = n - 1 - (row - col);
if (colUsed[col] || diagonals45Used[diagonals45Idx] || diagonals135Used[diagonals135Idx]) {
continue;
}
nQueens[row][col] = 'Q';
colUsed[col] = diagonals45Used[diagonals45Idx] = diagonals135Used[diagonals135Idx] = true;
backstracking(row + 1);
colUsed[col] = diagonals45Used[diagonals45Idx] = diagonals135Used[diagonals135Idx] = false;
nQueens[row][col] = '.';
}
}
```
### Backtracking
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DFS <20><>һ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>ͼ<EFBFBD>Ľڵ<C4BD><DAB5>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> **<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>** <20><><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> { 'a','b','c' } <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>еõ<D0B5><C3B5><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>ڳ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD><EFBFBD>б<EFBFBD><EFBFBD>ǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õݹ<EFBFBD>ʵ<EFBFBD>ֵĻ<EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD>ڷ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>µĵݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵݹ鷵<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>Ϊδ<EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD>Ϊֻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ͬ<EFBFBD>ĵݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD>ڵ<EFBFBD>ǰ<EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ء<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD>ּ<EFBFBD><D6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 17. Letter Combinations of a Phone Number (Medium)](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a3f34241-bb80-4879-8ec9-dff2d81b514e.jpg)
2018-02-13 23:29:52 +08:00
```html
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
```
```java
private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> ret = new ArrayList<>();
if (digits != null && digits.length() != 0) {
combination("", digits, 0, ret);
}
return ret;
}
private void combination(String prefix, String digits, int offset, List<String> ret) {
if (offset == digits.length()) {
ret.add(prefix);
return;
}
String letters = KEYS[digits.charAt(offset) - '0'];
for (char c : letters.toCharArray()) {
combination(prefix + c, digits, offset + 1, ret);
}
}
```
2018-02-22 14:47:54 +08:00
**<2A>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD>Ѱ<EFBFBD><D1B0><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 79. Word Search (Medium)](https://leetcode.com/problems/word-search/description/)
```html
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
```
```java
private static int[][] shift = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private static boolean[][] visited;
private int m;
private int n;
public boolean exist(char[][] board, String word) {
if (word == null || word.length() == 0) return true;
if (board == null || board.length == 0 || board[0].length == 0) return false;
m = board.length;
n = board[0].length;
visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dfs(board, word, 0, i, j)) return true;
}
}
return false;
}
private boolean dfs(char[][] board, String word, int start, int r, int c) {
if (start == word.length()) {
return true;
}
if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] != word.charAt(start) || visited[r][c] ) {
return false;
}
visited[r][c] = true;
for (int i = 0; i < shift.length; i++) {
int nextR = r + shift[i][0];
int nextC = c + shift[i][1];
if (dfs(board, word, start + 1, nextR, nextC)) return true;
}
visited[r][c] = false;
return false;
}
```
2018-02-22 14:47:54 +08:00
**IP <20><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 93. Restore IP Addresses(Medium)](https://leetcode.com/problems/restore-ip-addresses/description/)
```html
Given "25525511135",
return ["255.255.11.135", "255.255.111.35"].
```
```java
private List<String> ret;
public List<String> restoreIpAddresses(String s) {
ret = new ArrayList<>();
doRestore(0, "", s);
return ret;
}
private void doRestore(int k, String path, String s) {
if (k == 4 || s.length() == 0) {
if (k == 4 && s.length() == 0) {
ret.add(path);
}
return;
}
for (int i = 0; i < s.length() && i <= 2; i++) {
if (i != 0 && s.charAt(0) == '0') break;
String part = s.substring(0, i + 1);
if (Integer.valueOf(part) <= 255) {
doRestore(k + 1, path.length() != 0 ? path + "." + part : part, s.substring(i + 1));
}
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 46. Permutations (Medium)](https://leetcode.com/problems/permutations/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ret = new ArrayList<>();
List<Integer> permuteList = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
backtracking(permuteList, visited, nums, ret);
return ret;
}
private void backtracking(List<Integer> permuteList, boolean[] visited, int[] nums, List<List<Integer>> ret){
if(permuteList.size() == nums.length){
ret.add(new ArrayList(permuteList));
return;
}
for(int i = 0; i < visited.length; i++){
if(visited[i]) continue;
visited[i] = true;
permuteList.add(nums[i]);
backtracking(permuteList, visited, nums, ret);
permuteList.remove(permuteList.size() - 1);
visited[i] = false;
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 47. Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/)
```html
[1,1,2] have the following unique permutations:
[[1,1,2], [1,2,1], [2,1,1]]
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD> Permutations <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>ʱ<EFBFBD><CAB1><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ǰһ<C7B0><D2BB>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD>ǰһ<C7B0><D2BB>Ԫ<EFBFBD>ػ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ء<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ret = new ArrayList<>();
List<Integer> permuteList = new ArrayList<>();
Arrays.sort(nums);
boolean[] visited = new boolean[nums.length];
backtracking(permuteList, visited, nums, ret);
return ret;
}
private void backtracking(List<Integer> permuteList, boolean[] visited, int[] nums, List<List<Integer>> ret) {
if (permuteList.size() == nums.length) {
ret.add(new ArrayList(permuteList));
return;
}
for (int i = 0; i < visited.length; i++) {
if (i != 0 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
if (visited[i]) continue;
visited[i] = true;
permuteList.add(nums[i]);
backtracking(permuteList, visited, nums, ret);
permuteList.remove(permuteList.size() - 1);
visited[i] = false;
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 77. Combinations (Medium)](https://leetcode.com/problems/combinations/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD> 1 \~ n <20><>ȡ<EFBFBD><C8A1> k <20><>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD>ϡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ret = new ArrayList<>();
List<Integer> combineList = new ArrayList<>();
backtracking(1, n, k, combineList, ret);
return ret;
}
private void backtracking(int start, int n, int k, List<Integer> combineList, List<List<Integer>> ret){
if(k == 0){
2018-02-22 14:47:54 +08:00
ret.add(new ArrayList(combineList)); // <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>¹<EFBFBD><C2B9><EFBFBD>һ<EFBFBD><D2BB> List
2018-02-13 23:29:52 +08:00
return;
}
2018-02-22 14:47:54 +08:00
for(int i = start; i <= n - k + 1; i++){ // <20><>֦
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
combineList.add(i); // <20><> i <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>ѷ<EFBFBD><D1B7><EFBFBD>
2018-02-13 23:29:52 +08:00
backtracking(i + 1, n, k - 1, combineList, ret);
2018-02-22 14:47:54 +08:00
combineList.remove(combineList.size() - 1); // <20><> i <20><><EFBFBD><EFBFBD>Ϊδ<CEAA><CEB4><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 39. Combination Sum (Medium)](https://leetcode.com/problems/combination-sum/description/)
```html
given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[[7],[2, 2, 3]]
```
```java
private List<List<Integer>> ret;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
ret = new ArrayList<>();
doCombination(candidates, target, 0, new ArrayList<>());
return ret;
}
private void doCombination(int[] candidates, int target, int start, List<Integer> list) {
if (target == 0) {
ret.add(new ArrayList<>(list));
return;
}
for (int i = start; i < candidates.length; i++) {
if (candidates[i] <= target) {
list.add(candidates[i]);
doCombination(candidates, target - candidates[i], i, list);
list.remove(list.size() - 1);
}
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 40. Combination Sum II (Medium)](https://leetcode.com/problems/combination-sum-ii/description/)
```html
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
```
```java
private List<List<Integer>> ret;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
ret = new ArrayList<>();
Arrays.sort(candidates);
doCombination(candidates, target, 0, new ArrayList<>(), new boolean[candidates.length]);
return ret;
}
private void doCombination(int[] candidates, int target, int start, List<Integer> list, boolean[] visited) {
if (target == 0) {
ret.add(new ArrayList<>(list));
return;
}
for (int i = start; i < candidates.length; i++) {
if (i != 0 && candidates[i] == candidates[i - 1] && !visited[i - 1]) continue;
if (candidates[i] <= target) {
list.add(candidates[i]);
visited[i] = true;
doCombination(candidates, target - candidates[i], i + 1, list, visited);
visited[i] = false;
list.remove(list.size() - 1);
}
}
}
```
2018-02-22 14:47:54 +08:00
**<2A>Ӽ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 78. Subsets (Medium)](https://leetcode.com/problems/subsets/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>[1, 2] <20><> [2, 1] <20><><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD>ظ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private List<List<Integer>> ret;
private List<Integer> subsetList;
public List<List<Integer>> subsets(int[] nums) {
ret = new ArrayList<>();
subsetList = new ArrayList<>();
for (int i = 0; i <= nums.length; i++) {
backtracking(0, i, nums);
}
return ret;
}
private void backtracking(int startIdx, int size, int[] nums) {
if (subsetList.size() == size) {
ret.add(new ArrayList(subsetList));
return;
}
for (int i = startIdx; i < nums.length; i++) {
subsetList.add(nums[i]);
2018-02-22 14:47:54 +08:00
backtracking(i + 1, size, nums); // startIdx <20><>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB>Ԫ<EFBFBD>أ<EFBFBD>ʹ subset <20>е<EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
subsetList.remove(subsetList.size() - 1);
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>Ӽ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 90. Subsets II (Medium)](https://leetcode.com/problems/subsets-ii/description/)
```html
For example,
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
```
```java
private List<List<Integer>> ret;
private List<Integer> subsetList;
private boolean[] visited;
public List<List<Integer>> subsetsWithDup(int[] nums) {
ret = new ArrayList<>();
subsetList = new ArrayList<>();
visited = new boolean[nums.length];
Arrays.sort(nums);
for (int i = 0; i <= nums.length; i++) {
backtracking(0, i, nums);
}
return ret;
}
private void backtracking(int startIdx, int size, int[] nums) {
if (subsetList.size() == size) {
ret.add(new ArrayList(subsetList));
return;
}
for (int i = startIdx; i < nums.length; i++) {
if (i != 0 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
subsetList.add(nums[i]);
visited[i] = true;
backtracking(i + 1, size, nums);
visited[i] = false;
subsetList.remove(subsetList.size() - 1);
}
}
```
2018-02-22 14:47:54 +08:00
**<2A>ָ<EFBFBD><D6B8>ַ<EFBFBD><D6B7><EFBFBD>ʹ<EFBFBD><CAB9>ÿ<EFBFBD><C3BF><EFBFBD>ֶ<EFBFBD><D6B6>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 131. Palindrome Partitioning (Medium)](https://leetcode.com/problems/palindrome-partitioning/description/)
```java
private List<List<String>> ret;
public List<List<String>> partition(String s) {
ret = new ArrayList<>();
doPartion(new ArrayList<>(), s);
return ret;
}
private void doPartion(List<String> list, String s) {
if (s.length() == 0) {
ret.add(new ArrayList<>(list));
return;
}
for (int i = 0; i < s.length(); i++) {
if (isPalindrome(s, 0, i)) {
list.add(s.substring(0, i + 1));
doPartion(list, s.substring(i + 1));
list.remove(list.size() - 1);
}
}
}
private boolean isPalindrome(String s, int begin, int end) {
while (begin < end) {
if (s.charAt(begin++) != s.charAt(end--)) return false;
}
return true;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 37. Sudoku Solver (Hard)](https://leetcode.com/problems/sudoku-solver/description/)
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1ca52246-c443-48ae-b1f8-1cafc09ec75c.png)
2018-02-13 23:29:52 +08:00
```java
private boolean[][] rowsUsed = new boolean[9][10];
private boolean[][] colsUsed = new boolean[9][10];
private boolean[][] cubesUsed = new boolean[9][10];
private char[][] board;
public void solveSudoku(char[][] board) {
this.board = board;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') continue;
int num = board[i][j] - '0';
rowsUsed[i][num] = true;
colsUsed[j][num] = true;
cubesUsed[cubeNum(i, j)][num] = true;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
backtracking(i, j);
}
}
}
private boolean backtracking(int row, int col) {
while (row < 9 && board[row][col] != '.') {
row = col == 8 ? row + 1 : row;
col = col == 8 ? 0 : col + 1;
}
if (row == 9) {
return true;
}
for (int num = 1; num <= 9; num++) {
if (rowsUsed[row][num] || colsUsed[col][num] || cubesUsed[cubeNum(row, col)][num]) continue;
rowsUsed[row][num] = colsUsed[col][num] = cubesUsed[cubeNum(row, col)][num] = true;
board[row][col] = (char) (num + '0');
if (backtracking(row, col)) return true;
board[row][col] = '.';
rowsUsed[row][num] = colsUsed[col][num] = cubesUsed[cubeNum(row, col)][num] = false;
}
return false;
}
private int cubeNum(int i, int j) {
int r = i / 3;
int c = j / 3;
return r * 3 + c;
}
```
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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) {
int n = input.length();
List<Integer> ret = new ArrayList<>();
for (int i = 0; i < n; 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 '+': ret.add(l + r); break;
case '-': ret.add(l - r); break;
case '*': ret.add(l * r); break;
}
}
}
}
}
if (ret.size() == 0) ret.add(Integer.valueOf(input));
return ret;
}
```
2018-02-22 14:47:54 +08:00
## <20>ݹ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ԭʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD>ɽ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
## <20><>̬<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>򱣴<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ⣬ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>Ρ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
2018-02-13 23:29:52 +08:00
```java
public int integerBreak(int n) {
int[] dp = new int[n + 1];
dp[1] = 1;
for(int i = 2; i <= n; i++) {
for(int j = 1; j <= i - 1; j++) {
dp[i] = Math.max(dp[i], Math.max(j * dp[i - j], j * (i - j)));
}
}
return dp[n];
}
```
2018-02-22 14:47:54 +08:00
**<2A><>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
2018-02-13 23:29:52 +08:00
```java
public int numSquares(int n) {
2018-02-22 14:47:54 +08:00
List<Integer> squares = new ArrayList<>(); // <20>С<E6B4A2><D0A1> n <20><>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
int diff = 3;
while(square <= n) {
squares.add(square);
square += diff;
diff += 2;
}
int[] dp = new int[n + 1];
for(int i = 1; i <= n; i++) {
int max = Integer.MAX_VALUE;
for(int s : squares) {
if(s > i) break;
max = Math.min(max, dp[i - s] + 1);
}
dp[i] = max;
}
return dp[n];
}
```
2018-02-22 14:47:54 +08:00
**<2A>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
2018-02-13 23:29:52 +08:00
```java
public int numDecodings(String s) {
if(s == null || s.length() == 0) return 0;
int n = s.length();
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for(int i = 2; i <= n; i++) {
int one = Integer.valueOf(s.substring(i - 1, i));
if(one != 0) dp[i] += dp[i - 1];
if(s.charAt(i - 2) == '0') continue;
int two = Integer.valueOf(s.substring(i - 2, i));
if(two <= 26) dp[i] += dp[i - 2];
}
return dp[n];
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ƴӾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǵ<EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int uniquePaths(int m, int n) {
int[] dp = new int[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if(i == 0) dp[j] = 1;
else if(j != 0) dp[j] = dp[j] + dp[j - 1];
}
}
return dp[n - 1];
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С·<D0A1><C2B7><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǵ<EFBFBD><EFBFBD><EFBFBD>С·<EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>ÿ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int minPathSum(int[][] grid) {
if(grid.length == 0 || grid[0].length == 0) return 0;
int m = grid.length, n = grid[0].length;
int[] dp = new int[n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(j == 0) dp[0] = dp[0] + grid[i][0];
else if(i == 0) dp[j] = dp[j - 1] + grid[0][j];
else dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[n - 1];
}
```
2018-02-22 14:47:54 +08:00
### 쳲<><ECB3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ϵ<EFBFBD> 4 <20>µķ<C2B5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>СΪ O(lgN)<29><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>¥<EFBFBD><C2A5>**
2018-02-13 23:29:52 +08:00
[Leetcode : 70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N <20><>¥<EFBFBD>ݣ<EFBFBD>ÿ<EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>׻<EFBFBD><D7BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD><D7A3><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¥<EFBFBD>ݵķ<DDB5><C4B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp <20><EFBFBD><E6B4A2>¥<EFBFBD>ݵķ<DDB5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۣ<EFBFBD><DBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><C2B1><EFBFBD> 1 <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>dp[i] <20><>ʾ<EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD> i <20><>¥<EFBFBD>ݵķ<DDB5><C4B7><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD> i <20><>¥<EFBFBD>ݿ<EFBFBD><DDBF>Դӵ<D4B4> i-1 <20><> i-2 <20><>¥<EFBFBD><C2A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD> i <20><>¥<EFBFBD>ݵķ<DDB5><C4B7><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD> i-1 <20>͵<EFBFBD> i-2 <20><>¥<EFBFBD>ݵķ<DDB5><C4B7><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD>͡<EFBFBD>
2018-02-13 23:29:52 +08:00
<center>dp[i] = dp[i-1] + dp[i-2]</center>
2018-02-22 14:47:54 +08:00
dp[N] <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD> dp[i] ֻ<><D6BB> dp[i - 1] <20><> dp[i - 2] <20>йأ<D0B9><D8A3><EFBFBD><EFBFBD>˿<EFBFBD><CBBF><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢 dp[i - 1] <20><> dp[i - 2] <20><><EFBFBD>ɣ<EFBFBD>ʹ<EFBFBD><CAB9>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD> O(n) <20>ռ<D5BC>Ӷ<EFBFBD><D3B6>Ż<EFBFBD>Ϊ O(1) <20><><EFBFBD>Ӷȡ<D3B6>
2018-02-13 23:29:52 +08:00
```java
public int climbStairs(int n) {
if(n == 1) return 1;
if(n == 2) return 2;
2018-02-22 14:47:54 +08:00
// ǰһ<C7B0><D2BB>¥<EFBFBD>ݡ<EFBFBD><DDA1><EFBFBD>һ<EFBFBD><D2BB>¥<EFBFBD><C2A5>
2018-02-13 23:29:52 +08:00
int pre1 = 2, pre2 = 1;
for(int i = 2; i < n; i++){
int cur = pre1 + pre2;
pre2 = pre1;
pre1 = cur;
}
return pre1;
}
```
2018-02-22 14:47:54 +08:00
**ĸţ<C4B8><C5A3><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>-P181](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ũ<EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸţÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 ͷСĸţ<C4B8><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> 1 ֻСĸţ<C4B8><C5A3><EFBFBD>ӵڶ<D3B5><DAB6>ʼ<EABFAA><CABC>ĸţ<C4B8><C5A3>ʼ<EFBFBD><CABC>Сĸţ<C4B8><C5A3>ÿֻСĸţ 3 <20><>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>Сĸţ<C4B8><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N<><4E><EFBFBD><EFBFBD> N <20><><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>
2018-02-13 23:29:52 +08:00
<center>dp[i] = dp[i-1] + dp[i-3]</center>
2018-02-22 14:47:54 +08:00
**ǿ<><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ס<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD>ס<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i] <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><>ס<EFBFBD><D7A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD>ס<EFBFBD><D7A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> i <20><>ס<EFBFBD><D7A1><EFBFBD><EFBFBD>ôֻ<C3B4><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i - 2 <20><> i - 3 <20><>ס<EFBFBD><D7A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
![](http://latex.codecogs.com/gif.latex?\\\\dp[i]=max(dp[i-2],dp[i-3])+nums[i])
2018-02-22 14:47:54 +08:00
O(n) <20>ռ<D5BC>Ӷ<EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int rob(int[] nums) {
int n = nums.length;
if(n == 0) return 0;
if(n == 1) return nums[0];
if(n == 2) return Math.max(nums[0], nums[1]);
int[] dp = new int[n];
dp[0] = nums[0];
dp[1] = nums[1];
dp[2] = nums[0] + nums[2];
for(int i = 3; i < n; i++){
dp[i] = Math.max(dp[i -2], dp[i - 3]) + nums[i];
}
return Math.max(dp[n - 1], dp[n - 2]);
}
```
2018-02-22 14:47:54 +08:00
O(1) <20>ռ<D5BC>Ӷ<EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int rob(int[] nums) {
int n = nums.length;
if(n == 0) return 0;
if(n == 1) return nums[0];
if(n == 2) return Math.max(nums[0], nums[1]);
int pre3 = nums[0], pre2 = nums[1], pre1 = nums[2] + nums[0];
for(int i = 3; i < n; i++){
int cur = Math.max(pre2, pre3) + nums[i];
pre3 = pre2;
pre2 = pre1;
pre1 = cur;
}
return Math.max(pre1, pre2);
}
```
2018-02-22 14:47:54 +08:00
**ǿ<><C7BF><EFBFBD>ڻ<EFBFBD><DABB>ν<EFBFBD><CEBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/)
```java
public int rob(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int n = nums.length;
if(n == 1) return nums[0];
return Math.max(rob(nums, 0, n - 2), rob(nums, 1, n - 1));
}
private int rob(int[] nums, int s, int e) {
int n = nums.length;
if(e - s == 0) return nums[s];
if(e - s == 1) return Math.max(nums[s], nums[s + 1]);
int[] dp = new int[n];
dp[s] = nums[s];
dp[s + 1] = nums[s + 1];
dp[s + 2] = nums[s] + nums[s + 2];
for (int i = s + 3; i <= e; i++) {
dp[i] = Math.max(dp[i - 2], dp[i - 3]) + nums[i];
}
return Math.max(dp[e], dp[e - 1]);
}
```
2018-02-22 14:47:54 +08:00
**<2A>ż<EFBFBD><C5BC><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N <20><> <20><> <20><> <20>ŷ⣬<C5B7><E2A3AC><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>ŵķ<C5B5>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dp[i] <20><>ʾǰ i <20><><EFBFBD>ź<EFBFBD><C5BA>ŷ<EFBFBD><C5B7>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD> j <20><><EFBFBD>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD><EFBFBD><E6A3AC><EFBFBD><EFBFBD> j <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD> k <20><><EFBFBD>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD><EFBFBD><E6A1A3><EFBFBD><EFBFBD> i <20><> k <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> i==k<><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><> k <20><><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5>ź<EFBFBD><C5BA>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>λ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i-2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i-2] <20>ִ<EFBFBD><D6B4><EFBFBD>װ<EFBFBD>ŵķ<C5B5>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j <20><> i-1 <20><>ȡֵ<C8A1><D6B5><EFBFBD><EFBFBD><EFBFBD>˹<EFBFBD><CBB9><EFBFBD> (i-1)\*dp[i-2] <20>ִ<EFBFBD><D6B4><EFBFBD>װ<EFBFBD>ŷ<EFBFBD>ʽ<EFBFBD><CABD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> i != k<><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><> j <20><><EFBFBD>ź󣬵<C5BA> i <20><><EFBFBD>ź<EFBFBD><C5BA>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>λ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD> i-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i-1] <20>ִ<EFBFBD><D6B4><EFBFBD>װ<EFBFBD>ŷ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j <20><> i-1 <20><>ȡֵ<C8A1><D6B5><EFBFBD><EFBFBD><EFBFBD>˹<EFBFBD><CBB9><EFBFBD> (n-1)\*dp[i-1] <20>ִ<EFBFBD><D6B4><EFBFBD>װ<EFBFBD>ŷ<EFBFBD>ʽ<EFBFBD><CABD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
<center> dp[i] = (i-1) \* dp[i-2] + (i-1) \* dp[i-1] </center>
2018-02-22 14:47:54 +08:00
dp[N] <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>¥<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>dp[i] ֻ<><D6BB> dp[i-1] <20><> dp[i-2] <20>йأ<D0B9><D8A3><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢 dp[i-1] <20><> dp[i-2]<5D><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>֪һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {S<sub>1</sub>, S<sub>2</sub>,...,S<sub>n</sub>} <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD> {S<sub>i1</sub>, S<sub>i2</sub>,..., S<sub>im</sub>}<7D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i1<69><31>i2 ... im <20><><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>Ⱥ<EFBFBD>˳<EFBFBD>򣬳<EFBFBD><F2A3ACB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊԭ<CEAA><D4AD><EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB>**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**<2A><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD> ix > iy ʱ<><CAB1>S<sub>ix</sub> > S<sub>iy</sub><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊԭ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD>**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**<2A><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp <20><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3>ȣ<EFBFBD>dp[n] <20><>ʾ<EFBFBD><CABE> S<sub>n</sub> <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {S<sub>i1</sub>, S<sub>i2</sub>,...,S<sub>im</sub>}<7D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> im < n <EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>im</sub> < S<sub>n</sub> <20><><EFBFBD><EFBFBD>ʱ {S<sub>i1</sub>, S<sub>i2</sub>,..., S<sub>im</sub>, S<sub>n</sub>} Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><D0BE><EFBFBD>Ҫ<EFBFBD>ҵģ<D2B5><C4A3>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD> S<sub>n</sub> <20>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>n</sub> Ϊ<><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD> dp[n] = max{ dp[i]+1 | S<sub>i</sub> < S<sub>n</sub> && i < n} <EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[n] ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD><DEB7>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ʱ {S<sub>n</sub>} <20>͹<EFBFBD><CDB9><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2B7BD><EFBFBD><EFBFBD><EFBFBD>޸ģ<DEB8><C4A3><EFBFBD> dp[n] <20><>СΪ 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
![](http://latex.codecogs.com/gif.latex?\\\\dp[n]=max\{1,dp[i]+1|S_i<S_n\&\&i<n\})
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ N <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>N</sub> Ϊ<><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[N] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3>ȣ<EFBFBD><C8A3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> dp <20><><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> max{ dp[i] | 1 <= i <= N} <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/)
```java
public int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
for(int i = 0; i < n; i++){
int max = 1;
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]) max = Math.max(max, dp[j] + 1);
}
dp[i] = max;
}
int ret = 0;
for(int i = 0; i < n; i++){
ret = Math.max(ret, dp[i]);
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Ͻⷨ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(n<sup>2</sup>) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ö<EFBFBD><C3B6>ֲ<EFBFBD><D6B2><EFBFBD>ʹ<EFBFBD><CAB9>ʱ<EFBFBD><EFBFBD>ӶȽ<D3B6><C8BD><EFBFBD>Ϊ O(nlog<sub>n</sub>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> tails <20><><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD> tails[i] <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD>Ϊ i + 1 <20><><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [4,5,6,3]<5D><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```html
len = 1 : [4], [5], [6], [3] => tails[0] = 3
len = 2 : [4, 5], [5, 6] => tails[1] = 5
len = 3 : [4, 5, 6] => tails[2] = 6
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> x<><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> tails <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD> tails <20><><EFBFBD><EFBFBD><E6A3BB><EFBFBD><EFBFBD> tails[i-1] < x <= tails[i]<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD> tails[i] = x <EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD> tails <20><><EFBFBD><EFBFBD><E9B1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD> S<sub>i</sub> λ<><CEBB> tails <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ʱ<EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD>ʹ<EFBFBD>ö<EFBFBD><C3B6>ֲ<EFBFBD><D6B2>ҡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] tails = new int[n];
int size = 0;
for(int i = 0; i < n; i++){
int idx = binarySearch(tails, 0, size, nums[i]);
tails[idx] = nums[i];
if(idx == size) size++;
}
return size;
}
private int binarySearch(int[] nums, int sIdx, int eIdx, int key){
while(sIdx < eIdx){
int mIdx = sIdx + (eIdx - sIdx) / 2;
if(nums[mIdx] == key) return mIdx;
else if(nums[mIdx] > key) eIdx = mIdx;
else sIdx = mIdx + 1;
}
return sIdx;
}
```
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/)
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> O(n) ʱ<><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ up <20><> down<77><6E>
2018-02-13 23:29:52 +08:00
```java
public int wiggleMaxLength(int[] nums) {
int len = nums.length;
if (len == 0) return 0;
int up = 1, down = 1;
for (int i = 1; i < len; i++) {
if (nums[i] > nums[i - 1]) up = down + 1;
else if (nums[i] < nums[i - 1]) down = up + 1;
}
return Math.max(up, down);
}
```
2018-02-22 14:47:54 +08:00
### <20><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S1 <20><> S2<53><32><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD> dp[i][j] <20><>ʾ S1 <20><>ǰ i <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> S2 <20><>ǰ j <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD> S1<sub>i</sub> <20><> S2<sub>j</sub> ֵ<>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><> S1<sub>i</sub>==S2<sub>j</sub> ʱ<><CAB1><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S1 <20><>ǰ i-1 <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> S2 <20><>ǰ j-1 <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĻ<D0B5><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ټ<EFBFBD><D9BC><EFBFBD> S1<sub>i</sub> <20><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ȼ<EFBFBD> 1 <20><><EFBFBD><EFBFBD> dp[i][j] = dp[i-1][j-1] + 1<><31>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><> S1<sub>i</sub> != S2<sub>j</sub> ʱ<><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ S1 <20><>ǰ i-1 <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> S2 <20><>ǰ j <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD> S1 <20><>ǰ i <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> S2 <20><>ǰ j-1 <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD> dp[i][j] = max{ dp[i-1][j], dp[i][j-1] }<7D><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>е<EFBFBD>״̬ת<EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
![](http://latex.codecogs.com/gif.latex?\\\\
dp[i][j]=\left\{
\begin{array}{rcl}
dp[i-1][j-1]&&{S1_i==S2_j}\\
max(dp[i-1][j],dp[i][j-1])&&{S1_i<>S2_j}
\end{array}\right.
)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ڳ<EFBFBD><EFBFBD><EFBFBD>Ϊ N <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>1</sub> <20><> <20><><EFBFBD><EFBFBD>Ϊ M <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>2</sub><EFBFBD><EFBFBD>dp[N][M] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>1</sub> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>2</sub> <20><><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ȡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD>ͬ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>dp[i] <20><>ʾ<EFBFBD><CABE> S<sub>i</sub> Ϊ<><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S<sub>i</sub> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>dp[i][j] <20><>ʾ S1 <20><>ǰ i <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> S2 <20><>ǰ j <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ȣ<EFBFBD><C8A3><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> S1<sub>i</sub> <20><> S2<sub>j</sub> <20><>
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ս<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[N][M] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ս<D5BD><E2A3AC><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[N] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ս<D5BD><E2A3AC>Ϊ<EFBFBD><CEAA> S<sub>N</sub> Ϊ<><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> dp <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int lengthOfLCS(int[] nums1, int[] nums2) {
int n1 = nums1.length, n2 = nums2.length;
int[][] dp = new int[n1 + 1][n2 + 1];
for (int i = 1; i <= n1; i++) {
for (int j = 1; j <= n2; j++) {
if (nums1[i - 1] == nums2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[n1][n2];
}
```
2018-02-22 14:47:54 +08:00
### 0-1 <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ N <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>Ʒ<EFBFBD>ļ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD> w <20>ͼ<EFBFBD>ֵ v<><76>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i][j] <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>ǰ i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD>ܴﵽ<DCB4><EFB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>Ϊ w<><77><EFBFBD><EFBFBD>ֵΪ v<><76><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD> i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۣ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><> i <20><><EFBFBD><EFBFBD>Ʒû<C6B7><C3BB><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j <20><>ǰ i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j <20><>ǰ i-1 <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>dp[i][j] = dp[i-1][j]<5D><>
<EFBFBD><EFBFBD> <20><> i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>dp[i][j] = dp[i-1][j-w] + v<><76>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ӣ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ת<CCAC>Ʒ<EFBFBD><C6B7><EFBFBD>Ϊ<EFBFBD><CEAA>
2018-02-13 23:29:52 +08:00
![](http://latex.codecogs.com/gif.latex?\\\\dp[i][j]=max(dp[i-1][j],dp[i-1][j-w]+v))
```java
public int knapsack(int W, int N, int[] weights, int[] values) {
int[][] dp = new int[N][W];
for (int i = W - 1; i >= 0; i--) {
dp[0][i] = i > weights[0] ? values[0] : 0;
}
for (int i = 1; i < N; i++) {
for (int j = W - 1; j >= weights[i]; j--) {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i]);
}
for (int j = weights[i - 1] - 1; j >= 0; j--) {
dp[i][j] = dp[i - 1][j];
}
}
return dp[N - 1][W - 1];
}
```
2018-02-22 14:47:54 +08:00
**<2A>ռ<EFBFBD><D5BC>Ż<EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>ڳ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD> 0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD>۲<EFBFBD>״̬ת<CCAC>Ʒ<EFBFBD><C6B7>̿<EFBFBD><CCBF><EFBFBD>֪<EFBFBD><D6AA><EFBFBD><EFBFBD>ǰ i <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>״̬<D7B4><CCAC><EFBFBD><EFBFBD>ǰ i-1 <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>״̬<D7B4>йأ<D0B9><D8A3><EFBFBD><EFBFBD>˿<EFBFBD><CBBF>Խ<EFBFBD> dp <20><><EFBFBD><EFBFBD>Ϊһά<D2BB><CEAC><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD> dp[j] <20>ȿ<EFBFBD><C8BF>Ա<EFBFBD>ʾ dp[i-1][j] Ҳ<><D2B2><EFBFBD>Ա<EFBFBD>ʾ dp[i][j]<5D><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2018-02-13 23:29:52 +08:00
![](http://latex.codecogs.com/gif.latex?\\\\dp[j]=max(dp[j],dp[j-w]+v))
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ϊ dp[j-w] <20><>ʾ dp[i-1][j-w]<5D><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i][j-w] <20><>ֹ<EFBFBD><D6B9> dp[i-1][j-w] <20><><EFBFBD>ǡ<EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>˵Ҫ<CBB5>ȼ<EFBFBD><C8BC><EFBFBD> dp[i][j] <20>ټ<EFBFBD><D9BC><EFBFBD> dp[i][j-w]<5D><><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD>ʵ<EFBFBD><CAB5>ʱ<EFBFBD><CAB1>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>޷<EFBFBD>ʹ<EFBFBD><CAB9>̰<EFBFBD><CCB0><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>ʹ<EFBFBD><CAB9>̰<EFBFBD><CCB0><EFBFBD><EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD>⣬Ҳ<E2A3AC><D2B2><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ܰ<EFBFBD><DCB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ۱<D4BC><DBB1><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFB5BD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD><C9B1><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD>˷ѣ<CBB7><D1A3>Ӷ<EFBFBD><D3B6>޷<EFBFBD><DEB7><EFBFBD><EFB5BD><EFBFBD>š<EFBFBD><C5A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 5 <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ 1<><31><EFBFBD><EFBFBD>ôֻ<C3B4>ܴ<EFBFBD><DCB4>ŵļ<C5B5>ֵΪ 16<31><36><EFBFBD>˷<EFBFBD><CBB7>˴<EFBFBD>СΪ 2 <20>Ŀռ<D5BC><E4A1A3><EFBFBD>ŵķ<C5B5>ʽ<EFBFBD>Ǵ<EFBFBD><C7B4><EFBFBD><EFBFBD><EFBFBD>Ʒ 1 <20><><EFBFBD><EFBFBD>Ʒ 2<><32><EFBFBD><EFBFBD>ֵΪ 22.
2018-02-13 23:29:52 +08:00
2018-02-20 10:40:05 +08:00
| id | w | v | v/w |
| --- | --- | --- | --- |
| 0 | 1 | 6 | 6 |
| 1 | 2 | 10 | 5 |
| 2 | 3 | 12 | 4 |
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ 0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>ֵ<EFBFBD><D6B5>Ϊ 1/2/4... <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>Ȼ<EFBFBD><C8BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ʒֻ<C6B7><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>Ρ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ 0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ֮<EFBFBD><EFBFBD><EFBFBD>໥Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СΪ sum/2 <20><> 0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD>Ҳ<EFBFBD>в<EFBFBD>ͬ<EFBFBD>ĵط<C4B5><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>м<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB1BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>˿ռ<EFBFBD><EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum % 2 != 0) {
return false;
}
int W = sum / 2;
boolean[] dp = new boolean[W + 1];
int n = nums.length;
for(int i = 0; i <= W; i++) {
if(nums[0] == i) dp[i] = true;
}
for(int i = 1; i < n; i++) {
for(int j = W; j >= nums[i]; j--) {
dp[j] = dp[j] || dp[j - nums[i]];
}
}
return dp[W];
}
```
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1>ָ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/)
```html
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
```
```java
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (String word : wordDict) {
if (word.length() <= i
&& word.equals(s.substring(i - word.length(), i))) {
dp[i] = dp[i] || dp[i - word.length()];
}
}
}
return dp[n];
}
```
2018-02-22 14:47:54 +08:00
**<2A>ı<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD>ǵĺ<C7B5>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/)
```html
Input: nums is [1, 1, 1, 1, 1], S is 3.
Output: 5
Explanation:
-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3
There are 5 ways to assign symbols to make the sum of nums be target 3.
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ subset sum <20><><EFBFBD><EFBFBD>Ӷ<EFBFBD>ʹ<EFBFBD><CAB9> 0-1 <20><><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A1A3><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>P <20><> N<><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD> P ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>N ʹ<>ø<EFBFBD><C3B8>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>
2018-02-13 23:29:52 +08:00
```html
sum(P) - sum(N) = target
sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)
2 * sum(P) = target + sum(nums)
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻҪ<EFBFBD>ҵ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Һ͵<EFBFBD><EFBFBD><EFBFBD> (target + sum(nums))/2<><32><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD>ڽ⡣
2018-02-13 23:29:52 +08:00
```java
public int findTargetSumWays(int[] nums, int S) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum < S || (sum + S) % 2 == 1) {
return 0;
}
return subsetSum(nums, (sum + S) >>> 1);
}
private int subsetSum(int[] nums, int targetSum) {
Arrays.sort(nums);
int[] dp = new int[targetSum + 1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
for (int j = targetSum; j >= num; j--) {
dp[j] = dp[j] + dp[j - num];
}
}
return dp[targetSum];
}
```
2018-02-22 14:47:54 +08:00
**01<30>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/)
```html
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4
2018-02-22 14:47:54 +08:00
Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are <20><>10,<2C><>0001<30><31>,<2C><>1<EFBFBD><31>,<2C><>0<EFBFBD><30>
2018-02-13 23:29:52 +08:00
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> 0-1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int findMaxForm(String[] strs, int m, int n) {
if (strs == null || strs.length == 0) return 0;
int l = strs.length;
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i < l; i++) {
String s = strs[i];
int ones = 0, zeros = 0;
for (char c : s.toCharArray()) {
if (c == '0') zeros++;
else if (c == '1') ones++;
}
for (int j = m; j >= zeros; j--) {
for (int k = n; k >= ones; k--) {
if (zeros <= j && ones <= k) {
dp[j][k] = Math.max(dp[j][k], dp[j - zeros][k - ones] + 1);
}
}
}
}
return dp[m][n];
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>Ǯ**
2018-02-13 23:29:52 +08:00
[Leetcode : 322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD>ң<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ЩӲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>١<EFBFBD>Ӳ<EFBFBD>ҿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ʹ<EFBFBD>á<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0-1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>Ψһ<CEA8>IJ<EFBFBD>ͬ<EFBFBD>ǣ<EFBFBD><C7A3>ڶ<EFBFBD><DAB6><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>Ǵ<EFBFBD> 0 <20><>ʼ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
2018-02-13 23:29:52 +08:00
```java
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/)
```html
nums = [1, 2, 3]
target = 4
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Therefore the output is 7.
```
```java
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int j = 0; j < nums.length; j++) {
if(nums[j] <= i) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
```
2018-02-22 14:47:54 +08:00
**ֻ<>ܽ<EFBFBD><DCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>εĹ<CEB5>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
```java
public int maxProfit(int[] prices) {
int firstBuy = Integer.MIN_VALUE, firstSell = 0;
int secondBuy = Integer.MIN_VALUE, secondSell = 0;
for (int curPrice : prices) {
if (firstBuy < -curPrice) firstBuy = -curPrice;
if (firstSell < firstBuy + curPrice) firstSell = firstBuy + curPrice;
if (secondBuy < firstSell - curPrice) secondBuy = firstSell - curPrice;
if (secondSell < secondBuy + curPrice) secondSell = secondBuy + curPrice;
}
return secondSell;
}
```
2018-02-22 14:47:54 +08:00
**ֻ<>ܽ<EFBFBD><DCBD><EFBFBD> k <20>εĹ<CEB5>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
```html
dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] } = max(dp[i, j-1], prices[j] + max(dp[i-1, jj] - prices[jj]))
```
```java
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (k >= n/2) {
int maxPro = 0;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i-1])
maxPro += prices[i] - prices[i-1];
}
return maxPro;
}
int[][] dp = new int[k + 1][n];
for (int i = 1; i <= k; i++) {
int localMax = dp[i - 1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j - 1], prices[j] + localMax);
localMax = Math.max(localMax, dp[i - 1][j] - prices[j]);
}
}
return dp[k][n - 1];
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i \~ j <20>ĺͣ<C4BA><CDA3><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ sum[j] - sum[i-1]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sum[i] Ϊ 0 \~ j <20>ĺ͡<C4BA>
2018-02-13 23:29:52 +08:00
```java
class NumArray {
int[] nums;
public NumArray(int[] nums) {
for(int i = 1; i < nums.length; i++)
nums[i] += nums[i - 1];
this.nums = nums;
}
public int sumRange(int i, int j) {
return i == 0 ? nums[j] : nums[j] - nums[i - 1];
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> sum[i] Ϊ<><CEAA> num[i] Ϊ<><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺͣ<C4BA><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sum[i-1] <20>õ<EFBFBD> sum[i] <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sum[i-1] С<><D0A1> 0<><30><EFBFBD><EFBFBD>ô<EFBFBD><C3B4> num[i] Ϊ<><CEAA>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܰ<EFBFBD><DCB0><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>IJ<EFBFBD><C4B2>֣<EFBFBD><D6A3><EFBFBD>ô<EFBFBD><C3B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> num[i] <20><>С<EFBFBD><D0A1>
2018-02-13 23:29:52 +08:00
```java
public int maxSubArray(int[] nums) {
int n = nums.length;
int[] sum = new int[n];
sum[0] = nums[0];
int max = sum[0];
for(int i = 1; i < n; i++){
sum[i] = (sum[i-1] > 0 ? sum[i-1] : 0) + nums[i];
max = Math.max(max, sum[i]);
}
return max;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>еȲ<D0B5><C8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)
```html
A = [1, 2, 3, 4]
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> (1,2,3,4)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (1,2,3,4,5)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> (1,2,3,4) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2BBB9><EFBFBD><EFBFBD>һ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD> (1,2,3,4,5)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i] = dp[i - 1] + 1<><31>
2018-02-13 23:29:52 +08:00
```java
public int numberOfArithmeticSlices(int[] A) {
int n = A.length;
int[] dp = new int[n];
for(int i = 2; i < n; i++) {
if(A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
dp[i] = dp[i - 1] + 1;
}
}
int ret = 0;
for(int cnt : dp) {
ret += cnt;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
### <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int minDistance(String word1, String word2) {
int m = word1.length(), n = word2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) continue;
dp[i][j] = word1.charAt(i - 1) == word2.charAt(j - 1) ? dp[i - 1][j - 1] + 1
: Math.max(dp[i][j - 1], dp[i - 1][j]);
}
}
return m + n - 2 * dp[m][n];
}
```
2018-02-22 14:47:54 +08:00
**<2A>޸<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>** // TODO
2018-02-13 23:29:52 +08:00
[Leetcode : 72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/)
2018-02-22 14:47:54 +08:00
**<2A>޸<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>**// TODO
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>-<2D>ַ<EFBFBD><D6B7><EFBFBD>](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>Ҫ<EFBFBD><D2AA>ȴ<EFBFBD>ڵĹ<DAB5>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴʱ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ac9b31ec-cef1-4880-a875-fc4571ca10e1.png)
2018-02-13 23:29:52 +08:00
```html
s0[i] = max(s0[i - 1], s2[i - 1]); // Stay at s0, or rest from s2
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); // Stay at s1, or buy from s0
s2[i] = s1[i - 1] + prices[i]; // Only one way from s1
```
```java
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) return 0;
int n = prices.length;
int[] s0 = new int[n];
int[] s1 = new int[n];
int[] s2 = new int[n];
s0[0] = 0;
s1[0] = -prices[0];
s2[0] = Integer.MIN_VALUE;
for (int i = 1; i < n; i++) {
s0[i] = Math.max(s0[i - 1], s2[i - 1]);
s1[i] = Math.max(s1[i - 1], s0[i - 1] - prices[i]);
s2[i] = Math.max(s2[i - 1], s1[i - 1] + prices[i]);
}
return Math.max(s0[n - 1], s2[n - 1]);
}
```
2018-02-22 14:47:54 +08:00
**ͳ<>ƴ<EFBFBD> 0 \~ n ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD><CABE> 1 <20>ĸ<EFBFBD><C4B8><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6(110)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2(10) ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dp[i] = dp[i&(i-1)] + 1;
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
**һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EEB3A4>**
2018-02-13 23:29:52 +08:00
[Leetcode : 646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> (a, b) <20><> (c, d) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> b < c<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD>Թ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int findLongestChain(int[][] pairs) {
if(pairs == null || pairs.length == 0) {
return 0;
}
Arrays.sort(pairs, (a, b) -> (a[0] - b[0]));
int n = pairs.length;
int[] dp = new int[n];
Arrays.fill(dp, 1);
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
if(pairs[i][0] > pairs[j][1]){
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
int ret = 0;
for(int num : dp) {
ret = Math.max(ret, num);
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۳<EFBFBD><DBB3><EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
2018-02-22 14:47:54 +08:00
ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ν<EFBFBD><EFBFBD>ס<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ֻҪ<EFBFBD><EFBFBD>¼ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>۸񣬽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>۸<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۸<EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD>󽫵<EFBFBD>ǰ<EFBFBD>ļ۸<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>۳<EFBFBD><EFBFBD>۸񣬲鿴<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۸<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ǵ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۸<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD>ַ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/)
```java
public int minSteps(int n) {
int[] dp = new int[n + 1];
for (int i = 2; i <= n; i++) {
dp[i] = i;
for (int j = i - 1; j >= 0; j--) {
if (i % j == 0) {
dp[i] = dp[j] + dp[i / j];
break;
}
}
}
return dp[n];
}
```
```java
public int minSteps(int n) {
if (n == 1) return 0;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return i + minSteps(n / i);
}
return n;
}
```
2018-02-22 14:47:54 +08:00
## <20><>ѧ
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ÿһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Էֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij˻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 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> \* <20><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> x = 2<sup>m0</sup> \* 3<sup>m1</sup> \* 5<sup>m2</sup> \* 7<sup>m3</sup> \* 11<sup>m4</sup> \* <20><>
<EFBFBD><EFBFBD> y = 2<sup>n0</sup> \* 3<sup>n1</sup> \* 5<sup>n2</sup> \* 7<sup>n3</sup> \* 11<sup>n4</sup> \* <20><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> x <20><><EFBFBD><EFBFBD> y<><79>y mod x == 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i<><69>mi <= ni<6E><69>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
x <20><> y <20><> **<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD>** Ϊ<><CEAA>gcd(x,y) = 2<sup>min(m0,n0)</sup> \* 3<sup>min(m1,n1)</sup> \* 5<sup>min(m2,n2)</sup> \* ...
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
x <20><> y <20><> **<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>** Ϊ<><CEAA>lcm(x,y) = 2<sup>max(m0,n0)</sup> \* 3<sup>max(m1,n1)</sup> \* 5<sup>max(m2,n2)</sup> \* ...
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˹<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ų<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int countPrimes(int n) {
boolean[] notPrimes = new boolean[n + 1];
int cnt = 0;
for(int i = 2; i < n; i++){
if(notPrimes[i]) continue;
cnt++;
2018-02-22 14:47:54 +08:00
// <20><> i * i <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> k < i<><69><EFBFBD><EFBFBD>ô k * i <20><>֮ǰ<D6AE><C7B0><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
for(long j = (long) i * i; j < n; j += i){
notPrimes[(int) j] = true;
}
}
return cnt;
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>
2018-02-13 23:29:52 +08:00
```java
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>󹫱<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij˻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
int lcm(int a, int b){
return a * b / gcd(a, b);
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> a % b <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZȽϺ<C8BD>ʱ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> [ <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>2.7]() <20>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> a <20><> b <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC> f(a, b)<29><><EFBFBD>У<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
1\. <20><><EFBFBD><EFBFBD> a <20><> b <20><>Ϊż<CEAA><C5BC><EFBFBD><EFBFBD>f(a, b) = 2\*f(a/2, b/2);
2\. <20><><EFBFBD><EFBFBD> a <20><>ż<EFBFBD><C5BC> b <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>f(a, b) = f(a/2, b);
3\. <20><><EFBFBD><EFBFBD> b <20><>ż<EFBFBD><C5BC> a <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>f(a, b) = f(a, b/2);
4\. <20><><EFBFBD><EFBFBD> a <20><> b <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>f(a, b) = f(a, a-b);
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> 2 <20>ͳ<EFBFBD> 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Java <20><> static String toString(int num, int radix) <20><><EFBFBD>Խ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>Ϊ redix <20><><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**7 <20><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/)
```java
public String convertToBase7(int num) {
if (num < 0) {
return '-' + convertToBase7(-num);
}
if (num < 7) {
return num + "";
}
return convertToBase7(num / 7) + num % 7;
}
```
2018-02-22 14:47:54 +08:00
**16 <20><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/)
```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";
String ret = "";
while(num != 0){
ret = map[(num & 0b1111)] + ret;
num >>>= 4;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
### <20>׳<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**ͳ<>ƽ׳<C6BD>β<EFBFBD><CEB2><EFBFBD>ж<EFBFBD><D0B6>ٸ<EFBFBD> 0**
2018-02-13 23:29:52 +08:00
[Leetcode : 172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/)
2018-02-22 14:47:54 +08:00
β<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <20><> 2 * 5 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD> 5 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻҪͳ<D2AA><CDB3><EFBFBD>ж<EFBFBD><D0B6>ٸ<EFBFBD> 5 <20><><EFBFBD>ɡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> N<><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 5 <20>ĸ<EFBFBD><C4B8><EFBFBD>Ϊ<EFBFBD><CEAA>N/5 + N/5<sup>2</sup> + N/5<sup>3</sup> + ...<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N/5 <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 5 <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> 5<><35>N/5<sup>2</sup> <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> N <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 5<sup>2</sup> <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ٹ<EFBFBD><D9B9><EFBFBD>һ<EFBFBD><D2BB> 5 ...<2E><>
2018-02-13 23:29:52 +08:00
```java
public int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD> N! <20>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ 1 <20><>λ<EFBFBD>ã<EFBFBD>ֻҪͳ<D2AA><CDB3><EFBFBD>ж<EFBFBD><D0B6>ٸ<EFBFBD> 2 <20><><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD> [ <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>2.2](#) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>ٸ<EFBFBD> 5 һ<><D2BB><EFBFBD><EFBFBD>2 <20>ĸ<EFBFBD><C4B8><EFBFBD>Ϊ N/2 + N/2<sup>2</sup> + N/2<sup>3</sup> + ...
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>Ƽӷ<C6BC>**
2018-02-13 23:29:52 +08:00
[Leetcode : 67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/)
```java
public String addBinary(String a, String b) {
int i = a.length() - 1, j = b.length() - 1, carry = 0;
String str = "";
while(i >= 0 || j >= 0){
if(i >= 0 && a.charAt(i--) == '1') carry++;
if(j >= 0 && b.charAt(j--) == '1') carry++;
str = (carry % 2) + str;
carry /= 2;
}
if(carry == 1) str = "1" + str;
return str;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ӷ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ<EFBFBD>Ǹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int carry = 0;
for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
int x = i < 0 ? 0 : num1.charAt(i) - '0';
int y = j < 0 ? 0 : num2.charAt(j) - '0';
sb.append((x + y + carry) % 10);
carry = (x + y + carry) / 10;
}
return sb.reverse().toString();
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>ʹ<EFBFBD><CAB9><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>ο<EFBFBD><EFBFBD>Զ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؼ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ĸı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> m Ϊ<><CEAA>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>a <20><> b <20><> m <20><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD> b > a<><61>Ҫʹ a <20><> b <20><><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9>ƶ<EFBFBD><C6B6>Ĵ<EFBFBD><C4B4><EFBFBD>Ϊ b - a<><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> (b - m) + (m - a)<29><>Ҳ<EFBFBD><D2B2><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ N<><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD> N/2 <20><> a <20><> b <20><><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>Ƕ<EFBFBD><C7B6>ƶ<EFBFBD><C6B6><EFBFBD> m <20><>λ<EFBFBD>á<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ⷨ 1**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷȣ<EFBFBD>O(NlgN)
2018-02-13 23:29:52 +08:00
```java
public int minMoves2(int[] nums) {
Arrays.sort(nums);
int ret = 0;
int l = 0, h = nums.length - 1;
while(l <= h) {
ret += nums[h] - nums[l];
l++;
h--;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ⷨ 2**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD> O(N)
2018-02-13 23:29:52 +08:00
```java
public int minMoves2(int[] nums) {
int ret = 0;
int n = nums.length;
int median = quickSelect(nums, 0, n - 1, n / 2 + 1);
for(int num : nums) ret += Math.abs(num - median);
return ret;
}
private int quickSelect(int[] nums, int start, int end, int k) {
int l = start, r = end, privot = nums[(l + r) / 2];
while(l <= r) {
while(nums[l] < privot) l++;
while(nums[r] > privot) r--;
if(l >= r) break;
swap(nums, l, r);
l++; r--;
}
int left = l - start + 1;
if(left > k) return quickSelect(nums, start, l - 1, k);
if(left == k && l == r) return nums[l];
int right = r - start + 1;
return quickSelect(nums, r + 1, end, k - right);
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD>ͶƱ<CDB6><C6B1><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> n / 2 <20><>Ԫ<EFBFBD><D4AA>**
2018-02-13 23:29:52 +08:00
[Leetcode : 169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>ȶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD>Ǹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> n / 2
2018-02-13 23:29:52 +08:00
```java
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Boyer-Moore Majority Vote Algorithm <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<E2A3AC><CAB9>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(n)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B7A8>ʹ<EFBFBD><CAB9> cnt <20><>ͳ<EFBFBD><CDB3>һ<EFBFBD><D2BB>Ԫ<EFBFBD>س<EFBFBD><D8B3>ֵĴ<D6B5><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>غ<EFBFBD>ͳ<EFBFBD><CDB3>Ԫ<EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> cnt--<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD> cnt == 0 <20><>˵<EFBFBD><CBB5>ǰ i <20><>Ԫ<EFBFBD><D4AA>û<EFBFBD><C3BB> majority<74><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> majority<74><79><EFBFBD><EFBFBD><EFBFBD>dz<EFBFBD><C7B3>ֵĴ<D6B5><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i / 2 <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i / 2 <20>Ļ<EFBFBD> cnt <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 <20><><EFBFBD><EFBFBD>ʱʣ<CAB1>µ<EFBFBD> n - i <20><>Ԫ<EFBFBD><D4AA><EFBFBD>У<EFBFBD>majority <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD> (n - i) / 2<><32><EFBFBD><EFBFBD><EFBFBD>˼<EFBFBD><CBBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҿ<EFBFBD><D2BE><EFBFBD><EFBFBD>ҳ<EFBFBD> majority<74><79>
2018-02-13 23:29:52 +08:00
```java
public int majorityElement(int[] nums) {
int cnt = 0, majority = 0;
for(int i = 0; i < nums.length; i++){
if(cnt == 0) {
majority = nums[i];
cnt++;
}
else if(majority == nums[i]) cnt++;
else cnt--;
}
return majority;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> n / k <20><>Ԫ<EFBFBD><D4AA>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> P343](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ѡ k - 1 <20><><EFBFBD><EFBFBD>ѡ
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**ƽ<><C6BD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/)
2018-02-22 14:47:54 +08:00
ƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>1,4,9,16,..
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3,5,7,...
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>Ȳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD>Եõ<EFBFBD><EFBFBD><EFBFBD> 1 <20><>ʼ<EFBFBD><CABC>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean isPerfectSquare(int num) {
int subNum = 1;
while (num > 0) {
num -= subNum;
subNum += 2;
}
return num == 0;
}
```
2018-02-22 14:47:54 +08:00
**3 <20><> n <20>η<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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);
}
```
2018-02-22 14:47:54 +08:00
**<2A>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij˻<C4B3><CBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/)
```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);
}
```
2018-02-22 14:47:54 +08:00
**<2A>˻<EFBFBD><CBBB><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>Ϊԭʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD>˸<EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>Ԫ<EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صij˻<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ĿҪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(n)<29><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ʹ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] ret = new int[n];
ret[0] = 1;
for(int i = 1; i < n; i++) {
ret[i] = ret[i - 1] * nums[i - 1];
}
int right = 1;
for(int i = n - 1; i >= 0; i--) {
ret[i] *= right;
right *= nums[i];
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD>ݽṹ<DDBD><E1B9B9><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
## ջ<>Ͷ<EFBFBD><CDB6><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>ջʵ<D5BB>ֶ<EFBFBD><D6B6><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD>ջʵ<EFBFBD>֣<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
class MyQueue {
private Stack<Integer> st = new Stack();
public void push(int x) {
Stack<Integer> temp = new Stack();
while(!st.isEmpty()){
temp.push(st.pop());
}
st.push(x);
while(!temp.isEmpty()){
st.push(temp.pop());
}
}
public int pop() {
return st.pop();
}
public int peek() {
return st.peek();
}
public boolean empty() {
return st.isEmpty();
}
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջʵ<EFBFBD>֣<EFBFBD>
2018-02-13 23:29:52 +08:00
```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();
}
}
```
2018-02-22 14:47:54 +08:00
**<2A>ö<EFBFBD><C3B6><EFBFBD>ʵ<EFBFBD><CAB5>ջ**
2018-02-13 23:29:52 +08:00
[Leetcode : 225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/)
```java
class MyStack {
private Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.add(x);
2018-02-22 14:47:54 +08:00
for(int i = 1; i < queue.size(); i++){ // <EFBFBD><EFBFBD>ת
2018-02-13 23:29:52 +08:00
queue.add(queue.remove());
}
}
public int pop() {
return queue.remove();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><>Сֵջ**
2018-02-13 23:29:52 +08:00
[Leetcode : 155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջʵ<EFBFBD>֣<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сֵ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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);
if(x < min) {
min = x;
}
minStack.add(min);
}
public void pop() {
dataStack.pop();
minStack.pop();
if(!minStack.isEmpty()) {
min = minStack.peek();
} else{
min = Integer.MAX_VALUE;
}
}
public int top() {
return dataStack.peek();
}
public int getMin() {
return min;
}
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>Сֵջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>3.7<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>ջʵ<D5BB><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>**
2018-02-13 23:29:52 +08:00
[Leetcode : 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(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c == '(' || c == '{' || c == '[') stack.push(c);
else{
if(stack.isEmpty()) return false;
char cStack = stack.pop();
if(c == ')' && cStack != '(' ||
c == ']' && cStack != '[' ||
c == '}' && cStack != '{' ) {
return false;
}
}
}
return stack.isEmpty();
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>бȵ<D0B1>ǰԪ<C7B0>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صľ<D8B5><C4BE><EFBFBD>**
2018-02-13 23:29:52 +08:00
```html
Input: [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]
```
[Leetcode : 739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/)
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ء<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>֤<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] ret = new int[n];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < n; i++) {
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int idx = stack.pop();
ret[idx] = i - idx;
}
stack.add(i);
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ȵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 496. Next Greater Element I (Easy)](https://leetcode.com/problems/next-greater-element-i/description/)
```html
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
```
2018-02-22 14:47:54 +08:00
<EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD> Stack <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>˵<EFBFBD><CBB5>ջ<EFBFBD><D5BB>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>ǰԪ<C7B0>ء<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for(int num : nums2){
while(!stack.isEmpty() && num > stack.peek()){
map.put(stack.pop(), num);
}
stack.add(num);
}
int[] ret = new int[nums1.length];
for(int i = 0; i < nums1.length; i++){
if(map.containsKey(nums1[i])) ret[i] = map.get(nums1[i]);
else ret[i] = -1;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ȵ<EFBFBD>ǰԪ<C7B0>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/)
```java
public int[] nextGreaterElements(int[] nums) {
int n = nums.length, next[] = new int[n];
Arrays.fill(next, -1);
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n * 2; i++) {
int num = nums[i % n];
while (!stack.isEmpty() && nums[stack.peek()] < num)
next[stack.pop()] = num;
if (i < n) stack.push(i);
}
return next;
}
```
2018-02-22 14:47:54 +08:00
## <20><>ϣ<EFBFBD><CFA3>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash Table <20><><EFBFBD>Կ<EFBFBD><D4BF>ٲ<EFBFBD><D9B2><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫһ<D2AA><D2BB><EFBFBD>Ŀռ<C4BF><D5BC><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȿ<EFBFBD><C8BF><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷȵ<D3B6><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash Table <20><><EFBFBD>ֿռ任ȡʱ<C8A1><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Java <20>е<EFBFBD> **HashSet** <20><><EFBFBD>ڴ洢һ<E6B4A2><D2BB><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD> O(1) <20><>ʱ<EFBFBD><EFBFBD>ӶȲ<D3B6><C8B2><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>Ƿ<EFBFBD><C7B7>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҷ<EFBFBD>Χ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>Сд<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD>Ϳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 26 <20>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<E6B4A2><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ϣ<EFBFBD>ʹ<EFBFBD>ÿռ<D5BC>ӶȽ<D3B6><C8BD><EFBFBD>Ϊ O(1)<29><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Java <20>е<EFBFBD> **HashMap** <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>ڶ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HashMap <20><><EFBFBD>԰<EFBFBD>ԭʼ<D4AD><CABC><EFBFBD>ݺ<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> url <20><>ϵͳ<CFB5>У<EFBFBD>[Leetcdoe : 535. Encode and Decode TinyURL (Medium)](https://leetcode.com/problems/encode-and-decode-tinyurl/description/)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HashMap <20>Ϳ<EFBFBD><CDBF>Դ洢<D4B4><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> url <20><>ԭʼ url <20><>ӳ<EFBFBD>ʹ<E4A3AC>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>򻯵<EFBFBD> url<72><6C>Ҳ<EFBFBD><D2B2><EFBFBD>Ը<EFBFBD><D4B8>ݼ򻯵<DDBC> url <20>õ<EFBFBD>ԭʼ url <20>Ӷ<EFBFBD><D3B6><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
HashMap Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD><D8BD>м<EFBFBD><D0BC><EFBFBD>ͳ<EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>ʱ<EFBFBD><CAB1>ΪԪ<CEAA>أ<EFBFBD>ֵΪ<D6B5><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HashSet <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҷ<EFBFBD>Χ<EFBFBD><CEA7><EFBFBD>󣬿<EFBFBD><F3A3ACBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ơ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ֵ**
2018-02-13 23:29:52 +08:00
[Leetcode : 1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>˫ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD>ҷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(nlg<sub>n</sub>)<29><><EFBFBD>ռ<D5BC>Ӷ<EFBFBD>Ϊ O(1)<29><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> HashMap <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD>Ԫ<EFBFBD>غ<EFBFBD><D8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7>ʵ<EFBFBD> nums[i] ʱ<><CAB1><EFBFBD>ж<EFBFBD> HashMap <20><><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD> target - nums[i] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5> target - nums[i] <20><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(n)<29><><EFBFBD>ռ<D5BC>Ӷ<EFBFBD>Ϊ O(n)<29><>ʹ<EFBFBD>ÿռ<C3BF><D5BC><EFBFBD><EFBFBD><EFBFBD>ȡʱ<C8A1>
2018-02-13 23:29:52 +08:00
```java
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
if(map.containsKey(target - nums[i])) return new int[]{map.get(target - nums[i]), i};
else map.put(nums[i], i);
}
return null;
}
```
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD><EEB3A4>г<EFBFBD><D0B3><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1
2018-02-13 23:29:52 +08:00
[Leetcode : 594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/)
```java
public int findLHS(int[] nums) {
Map<Long, Integer> map = new HashMap<>();
for (long num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int result = 0;
for (long key : map.keySet()) {
if (map.containsKey(key + 1)) {
result = Math.max(result, map.get(key + 1) + map.get(key));
}
}
return result;
}
```
2018-02-22 14:47:54 +08:00
## <20>ַ<EFBFBD><D6B7><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Ƿ<EFBFBD><C7B7><EFBFBD>ȫ<EFBFBD><C8AB>ͬ**
2018-02-13 23:29:52 +08:00
[Leetcode : 242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сд<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD> 26 <20><>Сд<D0A1>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash Table <20><>ӳ<EFBFBD><D3B3><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ֵ<EFBFBD><D6B5>Χ<EFBFBD><CEA7>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD>ó<EFBFBD><EFBFBD><EFBFBD>Ϊ 26 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ƣ<EFBFBD><C6A3>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͬ<EFBFBD><CDAC>
2018-02-13 23:29:52 +08:00
```java
public boolean isAnagram(String s, String t) {
int[] cnts = new int[26];
for(int i = 0; i < s.length(); i++) cnts[s.charAt(i) - 'a'] ++;
for(int i = 0; i < t.length(); i++) cnts[t.charAt(i) - 'a'] --;
for(int i = 0; i < 26; i++) if(cnts[i] != 0) return false;
return true;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD>ͬ<EFBFBD><CDAC>**
2018-02-13 23:29:52 +08:00
[Leetcode : 205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> "egg" <20><> "add" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>¼һ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ϴγ<EFBFBD><EFBFBD>ֵ<EFBFBD>λ<EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ϴγ<EFBFBD><EFBFBD>ֵ<EFBFBD>λ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean isIsomorphic(String s, String t) {
int[] m1 = new int[256];
int[] m2 = new int[256];
for(int i = 0; i < s.length(); i++){
if(m1[s.charAt(i)] != m2[t.charAt(i)]) {
return false;
}
m1[s.charAt(i)] = i + 1;
m2[t.charAt(i)] = i + 1;
}
return true;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵĻ<C9B5><C4BB><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󳤶<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/)
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD>ó<EFBFBD><EFBFBD><EFBFBD>Ϊ 128 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3>ÿ<EFBFBD><C3BF><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ֵĸ<D6B5><C4B8><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ż<EFBFBD><C5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɻ<EFBFBD><C9BB><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD>Ǹ<EFBFBD><C7B8>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD>м䡣
2018-02-13 23:29:52 +08:00
```java
public int longestPalindrome(String s) {
2018-02-22 14:47:54 +08:00
int[] cnts = new int[128]; // ascii <20><><EFBFBD>ܹ<EFBFBD> 128 <20><>
2018-02-13 23:29:52 +08:00
for(char c : s.toCharArray()) cnts[c]++;
int ret = 0;
for(int cnt : cnts) ret += (cnt / 2) * 2;
2018-02-22 14:47:54 +08:00
if(ret < s.length()) ret ++; // <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> s <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>δʹ<EFBFBD>õ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>
2018-02-13 23:29:52 +08:00
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/)
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ䣬Ҳ<EFBFBD>Ͳ<EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD><EFBFBD>ұ<EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫת<EFBFBD>ã<EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean isPalindrome(int x) {
if(x == 0) return true;
if(x < 0) return false;
if(x % 10 == 0) return false;
int right = 0;
while(x > right){
right = right * 10 + x % 10;
x /= 10;
}
return x == right || x == right / 10;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijһλ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private int cnt = 0;
public int countSubstrings(String s) {
for(int i = 0; i < s.length(); i++) {
2018-02-22 14:47:54 +08:00
extendSubstrings(s, i, i); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
extendSubstrings(s, i, i + 1); // ż<><C5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
}
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++;
}
}
```
2018-02-22 14:47:54 +08:00
**ͳ<>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><> <20><><EFBFBD><EFBFBD> 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
```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".
```
[Leetcode : 696. Count Binary Substrings (Easy)](https://leetcode.com/problems/count-binary-substrings/description/)
```java
public int countBinarySubstrings(String s) {
int preLen = 0, curLen = 1, ret = 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) ret++;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[ <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>3.1](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD> s1 <20><> s2 <20><>Ҫ<EFBFBD><D2AA><EFBFBD>ж<EFBFBD> s2 <20>Ƿ<EFBFBD><C7B7>ܹ<EFBFBD><DCB9><EFBFBD> s1 <20><>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```html
s1 = AABCD, s2 = CDAA
Return : true
```
2018-02-22 14:47:54 +08:00
s1 <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>λ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD> s1s1 <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻҪ<D6BB>ж<EFBFBD> s2 <20>Ƿ<EFBFBD><C7B7><EFBFBD> s1s1 <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>λ**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[ <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>2.17](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD> k λ<><CEBB>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> abcd123 <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD> 3 λ <20>õ<EFBFBD> 123abcd
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> abcd123 <20>е<EFBFBD> abcd <20><> 123 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬵õ<F2A3ACB5> dcba321<32><31>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬵õ<F2A3ACB5>123abcd<63><64>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ʵķ<CAB5>ת**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>罫 "I am a student" <20><>ת<EFBFBD><D7AA> "student a am I"
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת** // TODO
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD><EFBFBD> P114](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**֮<><D6AE><EFBFBD>ʹ<EFBFBD>ӡ** //TODO
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> P335](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> 0 <20>Ƶ<EFBFBD>ĩβ**
2018-02-13 23:29:52 +08:00
[Leetcode : 283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/)
```java
public void moveZeroes(int[] nums) {
int n = nums.length;
int idx = 0;
for(int i = 0; i < n; i++){
if(nums[i] != 0) nums[idx++] = nums[i];
}
while(idx < n){
nums[idx++] = 0;
}
}
```
2018-02-22 14:47:54 +08:00
**һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD> [1, n] ֮<><EFBFBD><E4A3AC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻Ϊ<E6BBBB><CEAA>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD>ʧ<EFBFBD><CAA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ֱ<EFBFBD>ӵķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(nlog<sub>n</sub>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(n) <20><>ʱ<EFBFBD><EFBFBD>Ӷȡ<D3B6>O(1) <20>ռ<D5BC>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ҫ˼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ϡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i λ<>ϵ<EFBFBD>Ԫ<EFBFBD>ز<EFBFBD><D8B2><EFBFBD> i + 1 <20><><EFBFBD><EFBFBD>ô<EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD> i λ <20><> nums[i] - 1 λ<>ϵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>ʹ<EFBFBD><CAB9> num[i] - 1 <20><>Ԫ<EFBFBD><D4AA>Ϊ nums[i] <20><>Ҳ<EFBFBD><D2B2><EFBFBD>Ǹ<EFBFBD>λ<EFBFBD><CEBB>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD>ġ<EFBFBD><C4A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫѭ<D2AA><D1AD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>Ϊһ<CEAA>ν<EFBFBD><CEBD><EFBFBD>û<EFBFBD>ʹ<ECB7A8>õ<EFBFBD> i λ<>ϵ<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD>ġ<EFBFBD><C4A1><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؿ<EFBFBD><D8BF>ܾ<EFBFBD><DCBE><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD>ôѭ<C3B4><D1AD><EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>ֹѭ<D6B9><D1AD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>Ǽ<EFBFBD><C7BC><EFBFBD> nums[i] != nums[nums[i] - 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
- [Leetcode :448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)<29><>Ѱ<EFBFBD><D1B0><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ʧ<EFBFBD><CAA7>Ԫ<EFBFBD><D4AA>
- [Leetcode : 442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/)<29><>Ѱ<EFBFBD><D1B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>Ԫ<EFBFBD>ء<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int[] findErrorNums(int[] nums) {
for(int i = 0; i < nums.length; i++){
while(nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) swap(nums, i, nums[i] - 1);
}
for(int i = 0; i < nums.length; i++){
if(i + 1 != nums[i]) 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;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5> [0, n-1] ֮<><D6AE>**
2018-02-13 23:29:52 +08:00
[Leetcode : 287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD>ҽ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
˫ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><EFBFBD>зֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ö<EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD>ҷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```html
[
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
]
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetocde : 240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/)
```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;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Kth Element**
2018-02-13 23:29:52 +08:00
[Leetcode : 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.
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD>[Share my thoughts and Clean Java Code](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><EFBFBD>ҽ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD>ѽⷨ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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]));
2018-02-22 14:47:54 +08:00
for(int i = 0; i < k - 1; i++) { // С<EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD>ȥ<EFBFBD><EFBFBD> k - 1 <EFBFBD><EFBFBD><EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD>ؾ<EFBFBD><EFBFBD>ǵ<EFBFBD> k <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
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;
}
}
```
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
```html
2018-02-22 14:47:54 +08:00
A: a1 <20><> a2
<20>K
c1 <20><> c2 <20><> c3
<20>J
B: b1 <20><> b2 <20><> b3
2018-02-13 23:29:52 +08:00
```
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӷ<EFBFBD>Ϊ O(n) <20>ռ<D5BC>Ӷ<EFBFBD>Ϊ O(1)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> A <20>ij<EFBFBD><C4B3><EFBFBD>Ϊ a + c<><63>B <20>ij<EFBFBD><C4B3><EFBFBD>Ϊ b + c<><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD> c Ϊβ<CEAA><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ȣ<EFBFBD><C8A3><EFBFBD>֪ a + c + b = b + c + a<><61>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> A <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> B <20><>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> B<><42>ͬ<EFBFBD><CDAC><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> B <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> A <20><>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> A<><41><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܿ<EFBFBD><DCBF>Ʒ<EFBFBD><C6B7><EFBFBD> A <20><> B <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null) return null;
ListNode l1 = headA, l2 = headB;
while(l1 != l2){
l1 = (l1 == null) ? headB : l1.next;
l2 = (l2 == null) ? headA : l2.next;
}
return l1;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>3.6 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A1A3><EFBFBD><EFBFBD><EFBFBD>ֽⷨ<D6BD><E2B7A8><EFBFBD>ѵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD>β<EFBFBD><CEB2><EFBFBD>ӵ<EFBFBD><D3B5>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӱȽϵ<C8BD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD><DAB5>͵ڶ<CDB5><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD><DAB5>Ƿ<EFBFBD><C7B7><EFBFBD>ͬ<EFBFBD><CDAC>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת**
2018-02-13 23:29:52 +08:00
[Leetcode : 206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/)
2018-02-22 14:47:54 +08:00
ͷ<EFBFBD><EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򹹽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public ListNode reverseList(ListNode head) {
2018-02-22 14:47:54 +08:00
ListNode newHead = null; // <20><>Ϊ null <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD>β
2018-02-13 23:29:52 +08:00
while(head != null){
ListNode nextNode = head.next;
head.next = newHead;
newHead = head;
head = nextNode;
}
return newHead;
}
```
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD><E9B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õݹ鷽ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿսڵ㣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺ܶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
ListNode newHead = null;
if(l1.val < l2.val){
newHead = l1;
newHead.next = mergeTwoLists(l1.next, l2);
} else{
newHead = l2;
newHead.next = mergeTwoLists(l1, l2.next);
}
return newHead;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ظ<EFBFBD><D8B8>ڵ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/)
```java
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
head.next = deleteDuplicates(head.next);
return head.next != null && head.val == head.next.val ? head.next : head;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>г<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>η<EFBFBD>ת<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
2018-02-22 14:47:54 +08:00
if(fast != null){ // ż<><C5BC><EFBFBD>ڵ㣬<DAB5><E3A3AC> slow ָ<><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>
2018-02-13 23:29:52 +08:00
slow = slow.next;
}
2018-02-22 14:47:54 +08:00
cut(head, slow); // <20>г<EFBFBD><D0B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
ListNode l1 = head, l2 = slow;
l2 = reverse(l2);
return isEqual(l1, l2);
}
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;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ<EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<5B><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>3.4]()
2018-02-13 23:29:52 +08:00
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2c968ec5-0967-49ce-ac06-f8f5c9ab33bc.jpg)
2018-02-13 23:29:52 +08:00
```java
B.val = C.val;
B.next = C.next;
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>Ԫ<EFBFBD>ذ<EFBFBD><D8B0><EFBFBD>ż<EFBFBD>ۼ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/)
```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;
}
```
2018-02-22 14:47:54 +08:00
## <20><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
### <20>ݹ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫô<EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>룬ÿ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֵݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD>ĸ߶<C4B8>**
2018-02-13 23:29:52 +08:00
[Leetcode : 104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
```java
public int maxDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
```
2018-02-22 14:47:54 +08:00
**<2A><>ת<EFBFBD><D7AA>**
2018-02-13 23:29:52 +08:00
[Leetcode : 226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/)
```java
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
2018-02-22 14:47:54 +08:00
TreeNode left = root.left; // <20><><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD> left ָ<><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
root.left = invertTree(root.right);
root.right = invertTree(left);
return root;
}
```
2018-02-22 14:47:54 +08:00
**<2A><EFBFBD><E9B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/)
```java
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null && t2 == null) return null;
if(t1 == null) return t2;
if(t2 == null) return t1;
TreeNode root = new TreeNode(t1.val + t2.val);
root.left = mergeTrees(t1.left, t2.left);
root.right = mergeTrees(t1.right, t2.right);
return root;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> root <20><> leaf <20><><EFBFBD><EFBFBD><EFBFBD>нڵ<D0BD><DAB5>ĺ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
if(root.left == null && root.right == null && root.val == sum) return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
```
2018-02-22 14:47:54 +08:00
**ͳ<><CDB3>·<EFBFBD><C2B7><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> root <20><>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD> leaf <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
pathSumStartWithRoot() <20><><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ڵ㿪ͷ<E3BFAA><CDB7>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int pathSum(TreeNode root, int sum) {
if(root == null) return 0;
int ret = pathSumStartWithRoot(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
return ret;
}
private int pathSumStartWithRoot(TreeNode root, int sum){
if(root == null) return 0;
int ret = 0;
if(root.val == sum) ret++;
ret += pathSumStartWithRoot(root.left, sum - root.val) + pathSumStartWithRoot(root.right, sum - root.val);
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD>ĶԳ<C4B6>**
2018-02-13 23:29:52 +08:00
[Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
```java
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymmetric(root.left, root.right);
}
private boolean isSymmetric(TreeNode t1, TreeNode t2){
if(t1 == null && t2 == null) return true;
if(t1 == null || t2 == null) return false;
if(t1.val != t2.val) return false;
return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}
```
2018-02-22 14:47:54 +08:00
**ƽ<><C6BD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶Ȳ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD> 1
2018-02-13 23:29:52 +08:00
```java
private boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1) result = false;
return 1 + Math.max(l, r);
}
```
2018-02-22 14:47:54 +08:00
**<2A><>С·<D0A1><C2B7>**
2018-02-13 23:29:52 +08:00
[Leetcode : 111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD>ڵ㵽Ҷ<EFBFBD>ӽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int minDepth(TreeNode root) {
if(root == null) return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
if(left == 0 || right == 0) return left + right + 1;
return Math.min(left, right) + 1;
}
```
2018-02-22 14:47:54 +08:00
**ͳ<><CDB3><EFBFBD><EFBFBD>Ҷ<EFBFBD>ӽڵ<D3BD><DAB5>ĺ<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/)
```java
public int sumOfLeftLeaves(TreeNode root) {
if(root == null) return 0;
if(isLeaf(root.left)) return root.left.val + sumOfLeftLeaves(root.right);
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
private boolean isLeaf(TreeNode node){
if(node == null) return false;
return node.left == null && node.right == null;
}
```
2018-02-22 14:47:54 +08:00
**<2A>޼<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD> L \~ R ֮<><D6AE><EFBFBD>Ľڵ<C4BD>
2018-02-13 23:29:52 +08:00
```java
public TreeNode trimBST(TreeNode root, int L, int R) {
if(root == null) return null;
if(root.val > R) return trimBST(root.left, L, R);
if(root.val < L) return trimBST(root.right, L, R);
root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);
return root;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
```java
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s == null && t == null) return true;
if(s == null || t == null) return false;
if(s.val == t.val && isSame(s, t)) return true;
return isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean isSame(TreeNode s, TreeNode t){
if(s == null && t == null) return true;
if(s == null || t == null) return false;
if(s.val != t.val) return false;
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BST<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>нڵ㣬С<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>нڵ㡣
2018-02-13 23:29:52 +08:00
```java
public TreeNode sortedArrayToBST(int[] nums) {
return toBST(nums, 0, nums.length - 1);
}
private TreeNode toBST(int[] nums, int sIdx, int eIdx){
if(sIdx > eIdx) return null;
int mIdx = (sIdx + eIdx) / 2;
TreeNode root = new TreeNode(nums[mIdx]);
root.left = toBST(nums, sIdx, mIdx - 1);
root.right = toBST(nums, mIdx + 1, eIdx);
return root;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>·<EEB3A4><C2B7>**
2018-02-13 23:29:52 +08:00
```html
1
/ \
2 3
/ \
4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
```
```java
private int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return max;
}
private int depth(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = depth(root.left);
int rightDepth = depth(root.right);
max = Math.max(max, leftDepth + rightDepth);
return Math.max(leftDepth, rightDepth) + 1;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еڶ<D0B5>С<EFBFBD>Ľڵ<C4BD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/)
```html
Input:
2
/ \
2 5
/ \
5 7
Output: 5
```
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>Ҫô<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <20><><EFBFBD><EFBFBD> 2 <20><><EFBFBD>ӽڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ㣬<DAB5><E3A3AC>ô<EFBFBD><C3B4><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>Ľڵ㡣
2018-02-13 23:29:52 +08:00
```java
public int findSecondMinimumValue(TreeNode root) {
if(root == null) return -1;
if(root.left == null && root.right == null) return -1;
int leftVal = root.left.val;
int rightVal = root.right.val;
if(leftVal == root.val) leftVal = findSecondMinimumValue(root.left);
if(rightVal == root.val) rightVal = findSecondMinimumValue(root.right);
if(leftVal != -1 && rightVal != -1) return Math.min(leftVal, rightVal);
if(leftVal != -1) return leftVal;
return rightVal;
}
```
2018-02-22 14:47:54 +08:00
**Ѱ<><D1B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
```java
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
if(root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
return root;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
```java
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
return left == null ? right : right == null ? left : root;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD>ڵ<EFBFBD>ֵ<EFBFBD><D6B5>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 687. Longest Univalue Path (Easy)](https://pomotodo.com/app/)
```html
1
/ \
4 5
/ \ \
4 4 5
Output : 2
```
```java
private int path = 0;
public int longestUnivaluePath(TreeNode root) {
dfs(root);
return path;
}
private int dfs(TreeNode root){
if(root == null) return 0;
int left = dfs(root.left);
int right = dfs(root.right);
int leftPath = root.left != null && root.left.val == root.val ? left + 1 : 0;
int rightPath = root.right != null && root.right.val == root.val ? right + 1 : 0;
path = Math.max(path, leftPath + rightPath);
return Math.max(leftPath, rightPath);
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/)
```java
public int rob(TreeNode root) {
if (root == null) return 0;
int val1 = root.val;
if (root.left != null) {
val1 += rob(root.left.left) + rob(root.left.right);
}
if (root.right != null) {
val1 += rob(root.right.left) + rob(root.right.right);
}
int val2 = rob(root.left) + rob(root.right);
return Math.max(val1, val2);
}
```
2018-02-22 14:47:54 +08:00
### <20><><EFBFBD>α<EFBFBD><CEB1><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD> BFS<46><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<D2AA><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><E6B4A2>ǰ<EFBFBD><C7B0><EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ľڵ㣬 <20><>Ϊ<EFBFBD>ڿ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ľڵ<C4BD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>еĽڵ<C4BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD><EFBFBD><EFBFBD>ֻҪ<D6BB><D2AA><EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܱ<EFBFBD>֤<EFBFBD><D6A4><EFBFBD>α<EFBFBD><CEB1><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6>ǵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>Ľڵ㡣
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[637. Average of Levels in Binary Tree (Easy)](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
```java
public List<Double> averageOfLevels(TreeNode root) {
List<Double> ret = new ArrayList<>();
if(root == null) return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int cnt = queue.size();
double sum = 0;
for(int i = 0; i < cnt; i++){
TreeNode node = queue.poll();
sum += node.val;
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
ret.add(sum / cnt);
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>½ǵĽڵ<C4BD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/)
```java
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
root = queue.poll();
if(root.right != null) queue.add(root.right);
if(root.left != null) queue.add(root.left);
}
return root.val;
}
```
2018-02-22 14:47:54 +08:00
### ǰ<>к<EFBFBD><D0BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```html
1
/ \
2 3
/ \ \
4 5 6
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>α<EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>[1 2 3 4 5 6]
ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>[1 2 4 5 3 6]
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>[4 2 5 1 3 6]
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>[4 5 2 6 3 1]
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>α<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> BFS ʵ<>֣<EFBFBD><D6A3><EFBFBD><EFBFBD>õľ<C3B5><C4BE><EFBFBD> BFS һ<><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򡢺<EFBFBD><F2A1A2BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DFS ʵ<>֡<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򡢺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>ڶԽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>㲻ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> ǰ<><C7B0>
2018-02-13 23:29:52 +08:00
```java
void dfs(TreeNode root){
visit(root);
dfs(root.left);
dfs(root.right);
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
void dfs(TreeNode root){
dfs(root.left);
visit(root);
dfs(root.right);
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
void dfs(TreeNode root){
dfs(root.left);
dfs(root.right);
visit(root);
}
```
2018-02-22 14:47:54 +08:00
**<2A>ǵݹ<C7B5>ʵ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/)
```java
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
if (root == null) return ret;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
ret.add(node.val);
if (node.right != null) stack.push(node.right);
2018-02-22 14:47:54 +08:00
if (node.left != null) stack.push(node.left); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>
2018-02-13 23:29:52 +08:00
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ǵݹ<C7B5>ʵ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : ### 145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/)
2018-02-22 14:47:54 +08:00
ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ root -> left -> right<68><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ left -> right -> root<6F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ root -> right -> left<66><74><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>ͺͺ<CDBA><CDBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B7B4>
2018-02-13 23:29:52 +08:00
```java
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
if (root == null) return ret;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
ret.add(node.val);
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
Collections.reverse(ret);
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ǵݹ<C7B5>ʵ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/)
```java
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()) {
2018-02-22 14:47:54 +08:00
while(cur != null) { // ģ<><C4A3><EFBFBD>ݹ<EFBFBD>ջ<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
stack.add(cur);
cur = cur.left;
}
TreeNode node = stack.pop();
ret.add(node.val);
cur = node.right;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>** //TODO
2018-02-13 23:29:52 +08:00
### BST
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> BST <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص㡣
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A><> BST <20><>Ѱ<EFBFBD><D1B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㣬ʹ<E3A3AC><CAB9><EFBFBD>ǵĺ<C7B5>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>**
2018-02-13 23:29:52 +08:00
[653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/)
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD>ҡ<EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Ӧ<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱܷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean findTarget(TreeNode root, int k) {
List<Integer> nums = new ArrayList<>();
inOrder(root, nums);
int i = 0, j = nums.size() - 1;
while(i < j){
int sum = nums.get(i) + nums.get(j);
if(sum == k) return true;
if(sum < k) i++;
else j--;
}
return false;
}
private void inOrder(TreeNode root, List<Integer> nums){
if(root == null) return;
inOrder(root.left, nums);
nums.add(root.val);
inOrder(root.right, nums);
}
```
2018-02-22 14:47:54 +08:00
**<2A><> BST <20>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>ֵ**
2018-02-13 23:29:52 +08:00
[Leetcode : 530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> BST <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>ֵ<EFBFBD><D6B5>ȡ<EFBFBD><C8A1>Сֵ<D0A1><D6B5>
2018-02-13 23:29:52 +08:00
```java
private int minDiff = Integer.MAX_VALUE;
private int preVal = -1;
public int getMinimumDifference(TreeNode root) {
inorder(root);
return minDiff;
}
private void inorder(TreeNode node){
if(node == null) return;
inorder(node.left);
if(preVal != -1) minDiff = Math.min(minDiff, Math.abs(node.val - preVal));
preVal = node.val;
inorder(node.right);
}
```
2018-02-22 14:47:54 +08:00
**<2A><> BST ÿ<><C3BF><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD>ֵ**
2018-02-13 23:29:52 +08:00
[Leetcode : Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private int sum = 0;
public TreeNode convertBST(TreeNode root) {
traver(root);
return root;
}
private void traver(TreeNode root) {
if (root == null) {
return;
}
if (root.right != null) {
traver(root.right);
}
sum += root.val;
root.val = sum;
if (root.left != null) {
traver(root.left);
}
}
```
2018-02-22 14:47:54 +08:00
**Ѱ<><D1B0> BST <20>г<EFBFBD><D0B3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>**
2018-02-13 23:29:52 +08:00
```java
private int cnt = 1;
private int maxCnt = 1;
private TreeNode preNode = null;
private List<Integer> list;
public int[] findMode(TreeNode root) {
list = new ArrayList<>();
inorder(root);
int[] ret = new int[list.size()];
int idx = 0;
for(int num : list){
ret[idx++] = num;
}
return ret;
}
private void inorder(TreeNode node){
if(node == null) return;
inorder(node.left);
if(preNode != null){
if(preNode.val == node.val) cnt++;
else cnt = 1;
}
if(cnt > maxCnt){
maxCnt = cnt;
list.clear();
list.add(node.val);
} else if(cnt == maxCnt){
list.add(node.val);
}
preNode = node;
inorder(node.right);
}
```
2018-02-22 14:47:54 +08:00
**Ѱ<><D1B0> BST <20>ĵ<EFBFBD> k <20><>Ԫ<EFBFBD><D4AA>**
2018-02-13 23:29:52 +08:00
[Leetcode : 230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int kthSmallest(TreeNode root, int k) {
int leftCnt = count(root.left);
if(leftCnt == k - 1) return root.val;
if(leftCnt > k - 1) return kthSmallest(root.left, k);
return kthSmallest(root.right, k - leftCnt - 1);
}
private int count(TreeNode node) {
if(node == null) return 0;
return 1 + count(node.left) + count(node.right);
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
private int cnt = 0;
private int val;
public int kthSmallest(TreeNode root, int k) {
inorder(root, k);
return val;
}
private void inorder(TreeNode node, int k) {
if(node == null) return;
inorder(node.left, k);
cnt++;
if(cnt == k) {
val = node.val;
return;
}
inorder(node.right, k);
}
```
### Trie
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5c638d59-d4ae-4ba4-ad44-80bdc30f38dd.jpg)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Trie<EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD>ǰ׺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ǰ׺<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**ʵ<><CAB5>һ<EFBFBD><D2BB> Trie**
2018-02-13 23:29:52 +08:00
[Leetcode : 208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/)
```java
class Trie {
private class Node{
Node[] childs = new Node[26];
boolean isLeaf;
}
private Node root = new Node();
/** Initialize your data structure here. */
public Trie() {
}
/** Inserts a word into the trie. */
public void insert(String word) {
int idx = word.charAt(0) - 'a';
insert(word, root);
}
private void insert(String word, Node node){
int idx = word.charAt(0) - 'a';
if(node.childs[idx] == null){
node.childs[idx] = new Node();
}
if(word.length() == 1) node.childs[idx].isLeaf = true;
else insert(word.substring(1), node.childs[idx]);
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
return search(word, root);
}
private boolean search(String word, Node node){
if(node == null) return false;
int idx = word.charAt(0) - 'a';
if(node.childs[idx] == null) return false;
if(word.length() == 1) return node.childs[idx].isLeaf;
return search(word.substring(1), node.childs[idx]);
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
return startWith(prefix, root);
}
private boolean startWith(String prefix, Node node){
if(node == null) return false;
if(prefix.length() == 0) return true;
int idx = prefix.charAt(0) - 'a';
return startWith(prefix.substring(1), node.childs[idx]);
}
}
```
2018-02-22 14:47:54 +08:00
**ʵ<><CAB5>һ<EFBFBD><D2BB> Trie<69><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ׺<C7B0><D7BA>**
2018-02-13 23:29:52 +08:00
[Leetcode : 677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/)
```java
class MapSum {
private class Trie {
int val;
Map<Character, Trie> childs;
boolean isWord;
Trie() {
childs = new HashMap<>();
}
}
private Trie root;
public MapSum() {
root = new Trie();
}
public void insert(String key, int val) {
Trie cur = root;
for(char c : key.toCharArray()) {
if(!cur.childs.containsKey(c)) {
Trie next = new Trie();
cur.childs.put(c, next);
}
cur = cur.childs.get(c);
}
cur.val = val;
cur.isWord = true;
}
public int sum(String prefix) {
Trie cur = root;
for(char c : prefix.toCharArray()) {
if(!cur.childs.containsKey(c)) return 0;
cur = cur.childs.get(c);
}
return dfs(cur);
}
private int dfs(Trie cur) {
int sum = 0;
if(cur.isWord) {
sum += cur.val;
}
for(Trie next : cur.childs.values()) {
sum += dfs(next);
}
return sum;
}
}
```
2018-02-22 14:47:54 +08:00
## ͼ
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
## λ<><CEBB><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**1. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
0s <20><>ʾ һ<><D2BB> 0 <20><>1s <20><>ʾһ<CABE><D2BB> 1<><31>
2018-02-13 23:29:52 +08:00
```
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
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> x ^ 1s = \~x <20><><EFBFBD>ص㣬<D8B5><E3A3AC><EFBFBD>Խ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> x ^ x = 0 <20><><EFBFBD>ص㣬<D8B5><E3A3AC><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> x & 0s = 0 <20><> x & 1s = x <20><><EFBFBD>ص㣬<D8B5><E3A3AC><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> num <20><> mask <20><>00111100 <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD> num <20><><EFBFBD><EFBFBD> mask <20><> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>λ<EFBFBD><CEBB>
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> x | 0s = x <20><> x | 1s = 1s <20><><EFBFBD>ص㣬<D8B5><E3A3AC><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> num <20><> mask<73><6B>00111100 <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> num <20><><EFBFBD><EFBFBD> mask <20><> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1 <20><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
\>\> n Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD> 2<sup>n</sup><EFBFBD><EFBFBD>
\>\>\> n Ϊ<>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD>߻Ჹ<DFBB><E1B2B9> 0<><30>
&lt;&lt; n Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD> 2<sup>n</sup><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
n&(n-1) <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5> n <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ 10110**100**<2A><><EFBFBD><EFBFBD>ȥ 1 <20>õ<EFBFBD> 10110**011**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> 10110**000**<2A><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
n-n&(\~n+1) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5> n <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>һλ<D2BB><CEBB>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
n&(-n) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> n <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>һλ<D2BB><CEBB>-n <20>õ<EFBFBD> n <20>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ 10110**100**<2A><>-n <20>õ<EFBFBD> 01001**100**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> 00000**100**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**2. mask <20><><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD><EFBFBD>ȡ 111111111<31><31><EFBFBD><EFBFBD> 0 ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD>\~0<><30>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD>õ<EFBFBD>ֻ<EFBFBD>е<EFBFBD> i λΪ 1 <20><> mask<73><6B><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD> i λ<><CEBB><EFBFBD>ɣ<EFBFBD>1&lt;&lt;i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1&lt;&lt;5 <20>õ<EFBFBD>ֻ<EFBFBD>е<EFBFBD> 5 λΪ 1 <20><> mask <20><>00010000<30><30>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD>õ<EFBFBD> 1 <20><> i λΪ 1 <20><> mask<73><6B>1&lt;&lt;(i+1)-1 <20><><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD>罫 1&lt;&lt;(4+1)-1 = 00010000-1 = 00001111<31><31>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
Ҫ<EFBFBD>õ<EFBFBD> 1 <20><> i λΪ 0 <20><> mask<73><6B>ֻ<EFBFBD>轫 1 <20><> i λΪ 1 <20><> mask ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \~(1&lt;&lt;(i+1)-1)<29><>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**3. λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><>ȡ<EFBFBD><C8A1> i λ
2018-02-13 23:29:52 +08:00
num & 00010000 != 0
```java
(num & (1 << i)) != 0;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> i λ<><CEBB><EFBFBD><EFBFBD>Ϊ 1
2018-02-13 23:29:52 +08:00
num | 00010000
```java
num | (1 << i);
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> i λ<><CEBB><EFBFBD><EFBFBD>Ϊ 0
2018-02-13 23:29:52 +08:00
num & 11101111
```java
num & (\~(1 << i))
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD> i λ<><CEBB><EFBFBD><EFBFBD>Ϊ 0
2018-02-13 23:29:52 +08:00
num & 00001111
```java
num & ((1 << i) - 1);
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> 0 λ<><CEBB><EFBFBD><EFBFBD> i λ<><CEBB><EFBFBD><EFBFBD>Ϊ 0
2018-02-13 23:29:52 +08:00
num & 11110000
```java
num & (\~((1 << (i+1)) - 1));
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> i λ<><CEBB><EFBFBD><EFBFBD>Ϊ 0 <20><><EFBFBD><EFBFBD> 1
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD> i λ<><CEBB><EFBFBD>㣬Ȼ<E3A3AC><C8BB><EFBFBD><EFBFBD> v <20><><EFBFBD><EFBFBD> i λ<><CEBB>ִ<EFBFBD>С<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
(num & (1 << i)) | (v << i);
```
2018-02-22 14:47:54 +08:00
**4. Java <20>е<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
```html
2018-02-22 14:47:54 +08:00
static int Integer.bitCount() // ͳ<><CDB3> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static int Integer.highestOneBit() // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
static String toBinaryString(int i) // ת<><D7AA>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
2018-02-13 23:29:52 +08:00
```
2018-02-22 14:47:54 +08:00
**ͳ<><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD>ж<EFBFBD><D0B6><EFBFBD>λ<EFBFBD><CEBB>ͬ**
2018-02-13 23:29:52 +08:00
[Leetcode : 461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1 <20><>ͳ<EFBFBD><CDB3><EFBFBD>ж<EFBFBD><D0B6>ٸ<EFBFBD> 1 <20><><EFBFBD>ɡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> Integer.bitcount() <20><>ͳ<EFBFBD><CDB3> 1 <20><><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
```
2018-02-22 14:47:54 +08:00
**<2A><>תһ<D7AA><D2BB><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>λ**
2018-02-13 23:29:52 +08:00
[Leetcode : 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;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> <20><>P317](#)
2018-02-13 23:29:52 +08:00
```java
a = a ^ b;
b = a ^ b;
a = a ^ b;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> c = a ^ b<><62><EFBFBD><EFBFBD>ô b ^ c = b ^ b ^ a = a<><61>a ^ c = a ^ a ^ b = b<><62>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD> 4 <20><> n <20>η<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λΪ 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD>Ϊ 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 16 <20><> 10000<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>ΰ<EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD> 2 λ<><CEBB><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD>ȽϹ<C8BD><CFB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>жϵ<D0B6><CFB5><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͬ<EFBFBD><CDAC>
2018-02-13 23:29:52 +08:00
```java
public boolean isPowerOfFour(int num) {
int i = 1;
while(i > 0){
if(i == num) return true;
i = i << 2;
}
return false;
}
```
2018-02-22 14:47:54 +08:00
Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Java <20><> Integer.toString() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ 4 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD>ж<EFBFBD><D0B6>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD> 1 <20><>ͷ<EFBFBD><CDB7>
2018-02-13 23:29:52 +08:00
```java
public boolean isPowerOfFour(int num) {
return Integer.toString(num, 4).matches("10*");
}
```
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD> 2 <20><> n <20>η<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/)
2018-02-22 14:47:54 +08:00
ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Power of Four <20>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 <20><> n <20>η<EFBFBD><CEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾֻ<CABE><D6BB>һ<EFBFBD><D2BB> 1 <20><><EFBFBD>ڡ<EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public boolean isPowerOfTwo(int n) {
return n > 0 && Integer.bitCount(n) == 1;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1000 & 0111 == 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>½ⷨ<C2BD><E2B7A8>
2018-02-13 23:29:52 +08:00
```java
public boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ψһһ<D2BB><D2BB><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>Ԫ<EFBFBD><D4AA>**
2018-02-13 23:29:52 +08:00
[Leetcode : 136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>У<EFBFBD>[Leetcode : 389. Find the Difference (Easy)](https://leetcode.com/problems/find-the-difference/description/)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(1) <20>Ŀռ<D5BC>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<D2AA><CAB9> HashSet<65><74>
2018-02-13 23:29:52 +08:00
```java
public int singleNumber(int[] nums) {
int ret = 0;
for(int n : nums) ret = ret ^ n;
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>**
2018-02-13 23:29:52 +08:00
[Leetcode : 260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>ϱض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ͬ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
diff &= -diff <20>õ<EFBFBD><C3B5><EFBFBD> diff <20><><EFBFBD>Ҳ಻Ϊ 0 <20><>λ<EFBFBD><CEBB>Ҳ<EFBFBD><D2B2><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>Ҳ಻ͬ<E0B2BB><CDAC><EFBFBD><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ<D2BB>Ϳ<EFBFBD><CDBF>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int[] singleNumber(int[] nums) {
int diff = 0;
for(int num : nums) diff ^= num;
2018-02-22 14:47:54 +08:00
// <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>һλ
2018-02-13 23:29:52 +08:00
diff &= -diff;
int[] ret = new int[2];
for(int num : nums) {
if((num & diff) == 0) ret[0] ^= num;
else ret[1] ^= num;
}
return ret;
}
```
2018-02-22 14:47:54 +08:00
**<2A>ж<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD>Ƿ񲻻<C7B7><F1B2BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <20><> 1**
2018-02-13 23:29:52 +08:00
[Leetcode : 693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 10101 <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD> 1 λ<>õ<EFBFBD> 1010 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>Ľ<EFBFBD><C4BD><EFBFBD>Ϊ 11111<31><31>
2018-02-13 23:29:52 +08:00
```java
public boolean hasAlternatingBits(int n) {
int a = (n ^ (n >> 1));
return (a & (a + 1)) == 0;
}
```
2018-02-22 14:47:54 +08:00
**<2A><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD> 0 <20><><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 00000101<30><31>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD> 00000111 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 00000111<31><31>
2018-02-13 23:29:52 +08:00
```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;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Java <20><> Integer.highestOneBit() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int findComplement(int num) {
if(num == 0) return 1;
int mask = Integer.highestOneBit(num);
mask = (mask << 1) - 1;
return num ^ mask;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 10000000 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>չ<EFBFBD><D5B9> 11111111<31><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```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);
}
```
2018-02-22 14:47:54 +08:00
**ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļӷ<C4BC>**
2018-02-13 23:29:52 +08:00
[Leetcode : 371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/)
2018-02-22 14:47:54 +08:00
a ^ b <20><>ʾû<CABE>п<EFBFBD><D0BF>ǽ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺͣ<C4BA>(a & b) << 1 <EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> (a & b) << 1 <EFBFBD><EFBFBD><EFBFBD>ұ߻<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ飬<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>ұߵ<EFBFBD> 0 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int getSum(int a, int b) {
return b == 0 ? a : getSum((a ^ b), (a & b) << 1);
}
```
2018-02-22 14:47:54 +08:00
**ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD>**
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
[<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> P319](#)
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
**<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>**
2018-02-13 23:29:52 +08:00
[Leetcode : 318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сд<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сд<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD> 26 λ<><CEBB><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> 32 λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢ÿ<E6B4A2><C3BF><EFBFBD>ַ<EFBFBD><D6B7>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>
2018-02-13 23:29:52 +08:00
```java
public int maxProduct(String[] words) {
int n = words.length;
if (n == 0) return 0;
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;
}
```
2018-02-22 14:47:54 +08:00
# <20>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>
2018-02-13 23:29:52 +08:00
- [Leetcode](https://leetcode.com/problemset/algorithms/?status=Todo)
2018-02-22 14:47:54 +08:00
- <20><>ָ Offer
- <20><><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD>
- <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE>
- <20><><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
- <20><><EFBFBD>ݽṹ<DDBD><E1B9B9><EFBFBD><EFBFBD><E3B7A8><EFBFBD><EFBFBD>
- <20>
- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD><CCB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD>ָ<EFBFBD><D6B8>
2018-02-13 23:29:52 +08:00