Skip to content

Commit c802291

Browse files
committed
e
1 parent 187eb93 commit c802291

21 files changed

+318
-160
lines changed

Java/Binary Tree Paths.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,42 @@
4545
Binary Tree Binary Tree Traversal Facebook Google
4646
*/
4747

48-
/**
49-
* Definition for a binary tree node.
50-
* public class TreeNode {
51-
* int val;
52-
* TreeNode left;
53-
* TreeNode right;
54-
* TreeNode(int x) { val = x; }
55-
* }
56-
*/
48+
// cleaner:
49+
class Solution {
50+
public List<String> binaryTreePaths(TreeNode root) {
51+
List<String> rst = new ArrayList<>();
52+
if (root == null) {
53+
return rst;
54+
}
55+
dfs(rst, new ArrayList<>(), root);
56+
return rst;
57+
}
58+
59+
public void dfs(List<String> rst, List<Integer> list, TreeNode node) {
60+
if (node == null) return;
61+
62+
list.add(node.val);
63+
if (node.left == null && node.right == null) {
64+
rst.add(convert(list));
65+
list.remove(list.size() - 1);
66+
return;
67+
}
68+
69+
dfs(rst, list, node.left);
70+
dfs(rst, list, node.right);
71+
list.remove(list.size() - 1);
72+
}
73+
74+
private String convert(List<Integer> list) {
75+
StringBuffer sb = new StringBuffer();
76+
for (int i = 0; i < list.size() - 1; i++) {
77+
sb.append(list.get(i) + "->");
78+
}
79+
sb.append(list.get(list.size() - 1));
80+
return sb.toString();
81+
}
82+
}
83+
5784
/*
5885
Basic dfs, pass along sb, List<String>.
5986
Save when root == null.

Java/Find All Anagrams in a String.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
E
22
1524100532
3-
tags: Hash Table
3+
tags: Hash Table, Sliding Window
44

55
Permutation in String 很像. 给短string p长string s.
66

@@ -13,6 +13,37 @@
1313
- 小心不要用一个int[] count 来技术 查0, 复杂度是O(n)
1414

1515
```
16+
/**
17+
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
18+
19+
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
20+
21+
The order of output does not matter.
22+
23+
Example 1:
24+
25+
Input:
26+
s: "cbaebabacd" p: "abc"
27+
28+
Output:
29+
[0, 6]
30+
31+
Explanation:
32+
The substring with start index = 0 is "cba", which is an anagram of "abc".
33+
The substring with start index = 6 is "bac", which is an anagram of "abc".
34+
Example 2:
35+
36+
Input:
37+
s: "abab" p: "ab"
38+
39+
Output:
40+
[0, 1, 2]
41+
42+
Explanation:
43+
The substring with start index = 0 is "ab", which is an anagram of "ab".
44+
The substring with start index = 1 is "ba", which is an anagram of "ab".
45+
The substring with start index = 2 is "ab", which is an anagram of "ab".
46+
*/
1647
/*
1748
Thoughts:
1849
1. Two pointers with range of p.length(). O(n)

Java/Island Perimeter.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22
1516263328
33
tags: Hash Table
44

5-
最简单的方法: 每个格子4个墙;每个shared的墙要-2 (墙是两面, -1 * 2)
6-
最后合计结果就好.
5+
#### Brutle
6+
- 每个格子4个墙;每个shared的墙要-2 (墙是两面, -1 * 2)
7+
- 最后合计结果就好.
78

8-
不必想太多用HashMap做.但是也可以思考一下:
9+
#### Hash Table
10+
- 不必想太多用HashMap做.但是也可以思考一下:
911
- 把每个block相连的block全部存在以当下block为key的list里面. 那么这里需要把2D坐标, 转化成一个index.
1012
- 最后得到的map, 所有的key-value应该都有value-key的反向mapping, 那么久可以消除干净, 每一次消除就是一个shared wall.
1113
- 一点点optimization: DFS去找所有的island, 如果island的图非常大, 而island本身不打,那么适合optimize.
12-
但是整体代码过于复杂. 不建议写.
14+
- 但是整体代码过于复杂. 不建议写.
1315

1416

1517
```
1618
/*
17-
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
19+
You are given a map in form of a two-dimensional integer grid
20+
where 1 represents land and 0 represents water.
21+
Grid cells are connected horizontally/vertically (not diagonally).
22+
The grid is completely surrounded by water, and there is exactly one island
23+
(i.e., one or more connected land cells).
24+
25+
The island doesn't have "lakes" (water inside that isn't connected to the water around the island).
26+
One cell is a square with side length 1.
27+
28+
The grid is rectangular, width and height don't exceed 100.
29+
Determine the perimeter of the island.
1830
1931
Example:
2032
@@ -38,7 +50,6 @@ public int islandPerimeter(int[][] grid) {
3850
if (grid == null || grid.length == 0 || grid[0].length == 0) {
3951
return 0;
4052
}
41-
final Map<Integer, ArrayList<Integer>> map = new HashMap<>();
4253
final int[] dx = {1, -1, 0, 0};
4354
final int[] dy = {0, 0, 1, -1};
4455

Java/Linked List Cycle.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
1520009168
33
tags: Linked List, Two Pointers
44

5-
O(1) sapce: 用快慢指针一个跑.next, 一个跑.next.next总有一次fast会因为cycle而追上slow
6-
那个时候其实slow.val = fast.val.
5+
#### Two Pointer: Slow Fast Pointer
6+
- O(1) sapce: 用快慢指针一个跑.next, 一个跑.next.next总有一次fast会因为cycle而追上slow
7+
- 那个时候其实slow.val = fast.val.
8+
9+
#### Hash Table
10+
- O(n) space: 用HashMap一直add elements. 如果有重复那么很显然是有Cycle
711

8-
O(n) space: 用HashMap一直add elements. 如果有重复那么很显然是有Cycle
912
```
1013
/*
1114
50% Accepted

Java/Minimum Depth of Binary Tree.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
1525669253
33
tags: Tree, DFS, BFS
44

5+
#### BFS
6+
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
7+
- depth definition: reach to a leaf node, where node.left == null && node.right == null
8+
- BFS using queue, track level.
9+
10+
511
#### DFS
612
- Divide and Conquery一个最小值.
713
- 注意处理Leaf的null: null leaf 出现的时候, 就忽略这个leaf, 直接return算有leaf
814
- 另一种count的方法: 用Integer.MAX_VALUE代替 null leaf这样可以避免错误counting. (不能直接recursive)
915
- 这个无论如何都要走所有node, 所以dfs应该比较适合.
1016

11-
#### BFS
12-
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
13-
- depth definition: reach to a leaf node, where node.left == null && node.right == null
14-
- BFS using queue, track level.
1517

1618
```
1719
/*

Java/Path Sum III.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#### 特点
1515
- `Binary Tree Longest Consecutive Sequence II` 在recursive的做法上很相似:
1616
- 利用dfs做包括root的recursive computation
17-
- 利用这个function自己, 做不包括root的recursive computation
17+
- 利用这个function自己, `不包括root的recursive computation`
1818

1919
```
2020
/*

Java/Reverse Integer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ class Solution {
7676
public int reverse(int x) {
7777
long result = (long) x;
7878
char[] arr = (Math.abs(result) + "").toCharArray();
79-
int arrLength = arr.length;
80-
for (int i = 0; i < arrLength/2; i++) {
79+
int n = arr.length;
80+
for (int i = 0; i < n/2; i++) {
8181
char temp = arr[i];
82-
arr[i] = arr[arrLength - i - 1];
83-
arr[arrLength - i - 1] = temp;
82+
arr[i] = arr[n - i - 1];
83+
arr[n - i - 1] = temp;
8484
}
8585
result = Long.parseLong(String.valueOf(arr)) * (x > 0 ? 1 : -1);
8686
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {

LevelReviewPage.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,16 @@ O(n)
333333
**29. [Island Perimeter.java](https://github.com/awangdev/LintCode/blob/master/Java/Island%20Perimeter.java)** Level: Easy Tags: [Hash Table]
334334

335335

336-
最简单的方法: 每个格子4个墙;每个shared的墙要-2 (墙是两面, -1 * 2)
337-
最后合计结果就好.
336+
#### Brutle
337+
- 每个格子4个墙;每个shared的墙要-2 (墙是两面, -1 * 2)
338+
- 最后合计结果就好.
338339

339-
不必想太多用HashMap做.但是也可以思考一下:
340+
#### Hash Table
341+
- 不必想太多用HashMap做.但是也可以思考一下:
340342
- 把每个block相连的block全部存在以当下block为key的list里面. 那么这里需要把2D坐标, 转化成一个index.
341343
- 最后得到的map, 所有的key-value应该都有value-key的反向mapping, 那么久可以消除干净, 每一次消除就是一个shared wall.
342344
- 一点点optimization: DFS去找所有的island, 如果island的图非常大, 而island本身不打,那么适合optimize.
343-
但是整体代码过于复杂. 不建议写.
345+
- 但是整体代码过于复杂. 不建议写.
344346

345347

346348

@@ -739,10 +741,13 @@ HashMap
739741
**56. [Linked List Cycle.java](https://github.com/awangdev/LintCode/blob/master/Java/Linked%20List%20Cycle.java)** Level: Easy Tags: [Linked List, Two Pointers]
740742

741743

742-
O(1) sapce: 用快慢指针。一个跑.next, 一个跑.next.next。 总有一次,fast会因为cycle而追上slow。
743-
那个时候其实slow.val = fast.val.
744+
#### Two Pointer: Slow Fast Pointer
745+
- O(1) sapce: 用快慢指针。一个跑.next, 一个跑.next.next。 总有一次,fast会因为cycle而追上slow。
746+
- 那个时候其实slow.val = fast.val.
747+
748+
#### Hash Table
749+
- O(n) space: 用HashMap,一直add elements. 如果有重复,那么很显然是有Cycle
744750

745-
O(n) space: 用HashMap,一直add elements. 如果有重复,那么很显然是有Cycle
746751

747752

748753
---
@@ -1181,7 +1186,7 @@ space: O(n) or rolling array O(1)
11811186

11821187
---
11831188

1184-
**82. [Find All Anagrams in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20All%20Anagrams%20in%20a%20String.java)** Level: Easy Tags: [Hash Table]
1189+
**82. [Find All Anagrams in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20All%20Anagrams%20in%20a%20String.java)** Level: Easy Tags: [Hash Table, Sliding Window]
11851190

11861191

11871192
跟 Permutation in String 很像. 给短string p, 长string s.
@@ -1571,16 +1576,18 @@ space: O(n), O(1) rolling array
15711576
**107. [Minimum Depth of Binary Tree.java](https://github.com/awangdev/LintCode/blob/master/Java/Minimum%20Depth%20of%20Binary%20Tree.java)** Level: Easy Tags: [BFS, DFS, Tree]
15721577

15731578

1579+
#### BFS
1580+
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
1581+
- depth definition: reach to a leaf node, where node.left == null && node.right == null
1582+
- BFS using queue, track level.
1583+
1584+
15741585
#### DFS
15751586
- Divide and Conquery一个最小值.
15761587
- 注意处理Leaf的null: null leaf 出现的时候, 就忽略这个leaf, 直接return算有leaf
15771588
- 另一种count的方法: 用Integer.MAX_VALUE代替 null leaf,这样可以避免错误counting. (不能直接recursive)
15781589
- 这个无论如何都要走所有node, 所以dfs应该比较适合.
15791590

1580-
#### BFS
1581-
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
1582-
- depth definition: reach to a leaf node, where node.left == null && node.right == null
1583-
- BFS using queue, track level.
15841591

15851592

15861593

@@ -1863,7 +1870,7 @@ count所有存在的 path sum == target sum. 可以从任意点开始. 但是只
18631870
#### 特点
18641871
-`Binary Tree Longest Consecutive Sequence II` 在recursive的做法上很相似:
18651872
- 利用dfs做包括root的recursive computation
1866-
- 利用这个function自己, 做不包括root的recursive computation
1873+
- 利用这个function自己, `不包括root的recursive computation`
18671874

18681875

18691876

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
|221|[Permutation in String.java](https://github.com/awangdev/LintCode/blob/master/Java/Permutation%20in%20String.java)|Medium|Java|[Two Pointers]||
243243
|222|[Permutations II.java](https://github.com/awangdev/LintCode/blob/master/Java/Permutations%20II.java)|Medium|Java|[Backtracking]||
244244
|223|[Shuffle an Array.java](https://github.com/awangdev/LintCode/blob/master/Java/Shuffle%20an%20Array.java)|Medium|Java|[Permutation]||
245-
|224|[Find All Anagrams in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20All%20Anagrams%20in%20a%20String.java)|Easy|Java|[Hash Table]||
245+
|224|[Find All Anagrams in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20All%20Anagrams%20in%20a%20String.java)|Easy|Java|[Hash Table, Sliding Window]||
246246
|225|[Group Anagrams.java](https://github.com/awangdev/LintCode/blob/master/Java/Group%20Anagrams.java)|Medium|Java|[Hash Table, String]||
247247
|226|[Backpack.java](https://github.com/awangdev/LintCode/blob/master/Java/Backpack.java)|Medium|Java|[Backpack DP, DP]||
248248
|227|[Backpack II.java](https://github.com/awangdev/LintCode/blob/master/Java/Backpack%20II.java)|Medium|Java|[Backpack DP, DP]||

ReviewPage.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -729,14 +729,16 @@ O(n)
729729
**60. [Island Perimeter.java](https://github.com/awangdev/LintCode/blob/master/Java/Island%20Perimeter.java)** Level: Easy Tags: [Hash Table]
730730

731731

732-
最简单的方法: 每个格子4个墙;每个shared的墙要-2 (墙是两面, -1 * 2)
733-
最后合计结果就好.
732+
#### Brutle
733+
- 每个格子4个墙;每个shared的墙要-2 (墙是两面, -1 * 2)
734+
- 最后合计结果就好.
734735

735-
不必想太多用HashMap做.但是也可以思考一下:
736+
#### Hash Table
737+
- 不必想太多用HashMap做.但是也可以思考一下:
736738
- 把每个block相连的block全部存在以当下block为key的list里面. 那么这里需要把2D坐标, 转化成一个index.
737739
- 最后得到的map, 所有的key-value应该都有value-key的反向mapping, 那么久可以消除干净, 每一次消除就是一个shared wall.
738740
- 一点点optimization: DFS去找所有的island, 如果island的图非常大, 而island本身不打,那么适合optimize.
739-
但是整体代码过于复杂. 不建议写.
741+
- 但是整体代码过于复杂. 不建议写.
740742

741743

742744

@@ -2092,10 +2094,13 @@ HashMap的做法比char[]写起来要复杂一点, 但是更generic
20922094
**136. [Linked List Cycle.java](https://github.com/awangdev/LintCode/blob/master/Java/Linked%20List%20Cycle.java)** Level: Easy Tags: [Linked List, Two Pointers]
20932095

20942096

2095-
O(1) sapce: 用快慢指针。一个跑.next, 一个跑.next.next。 总有一次,fast会因为cycle而追上slow。
2096-
那个时候其实slow.val = fast.val.
2097+
#### Two Pointer: Slow Fast Pointer
2098+
- O(1) sapce: 用快慢指针。一个跑.next, 一个跑.next.next。 总有一次,fast会因为cycle而追上slow。
2099+
- 那个时候其实slow.val = fast.val.
2100+
2101+
#### Hash Table
2102+
- O(n) space: 用HashMap,一直add elements. 如果有重复,那么很显然是有Cycle
20972103

2098-
O(n) space: 用HashMap,一直add elements. 如果有重复,那么很显然是有Cycle
20992104

21002105

21012106
---
@@ -4089,7 +4094,7 @@ reset() 给出最初的nums
40894094

40904095
---
40914096

4092-
**224. [Find All Anagrams in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20All%20Anagrams%20in%20a%20String.java)** Level: Easy Tags: [Hash Table]
4097+
**224. [Find All Anagrams in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20All%20Anagrams%20in%20a%20String.java)** Level: Easy Tags: [Hash Table, Sliding Window]
40934098

40944099

40954100
跟 Permutation in String 很像. 给短string p, 长string s.
@@ -5001,16 +5006,18 @@ reverse 一个 linked list 中 [m ~ n] 的一部分.
50015006
**271. [Minimum Depth of Binary Tree.java](https://github.com/awangdev/LintCode/blob/master/Java/Minimum%20Depth%20of%20Binary%20Tree.java)** Level: Easy Tags: [BFS, DFS, Tree]
50025007

50035008

5009+
#### BFS
5010+
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
5011+
- depth definition: reach to a leaf node, where node.left == null && node.right == null
5012+
- BFS using queue, track level.
5013+
5014+
50045015
#### DFS
50055016
- Divide and Conquery一个最小值.
50065017
- 注意处理Leaf的null: null leaf 出现的时候, 就忽略这个leaf, 直接return算有leaf
50075018
- 另一种count的方法: 用Integer.MAX_VALUE代替 null leaf,这样可以避免错误counting. (不能直接recursive)
50085019
- 这个无论如何都要走所有node, 所以dfs应该比较适合.
50095020

5010-
#### BFS
5011-
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
5012-
- depth definition: reach to a leaf node, where node.left == null && node.right == null
5013-
- BFS using queue, track level.
50145021

50155022

50165023

@@ -5533,7 +5540,7 @@ count所有存在的 path sum == target sum. 可以从任意点开始. 但是只
55335540
#### 特点
55345541
-`Binary Tree Longest Consecutive Sequence II` 在recursive的做法上很相似:
55355542
- 利用dfs做包括root的recursive computation
5536-
- 利用这个function自己, 做不包括root的recursive computation
5543+
- 利用这个function自己, `不包括root的recursive computation`
55375544

55385545

55395546

0 commit comments

Comments
 (0)