Skip to content

Commit 7b83e85

Browse files
Merge pull request #13 from daikaixian/master
homework for 3rd class
2 parents 5725720 + 33f5bbd commit 7b83e85

File tree

9 files changed

+404
-0
lines changed

9 files changed

+404
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
13+
14+
//思路一: 和level order traversal 一样。 只是交替的变换一下顺序?
15+
16+
17+
Queue<TreeNode> parentQueue = new LinkedList<>();
18+
Queue<TreeNode> childrenQueue = new LinkedList<>();
19+
20+
if (root == null) {
21+
return new ArrayList<>();
22+
}
23+
24+
List<List<Integer>> list = new ArrayList<>();
25+
List<Integer> subList = new ArrayList<>();
26+
parentQueue.add(root);
27+
int level = 0;
28+
while (!parentQueue.isEmpty()) {
29+
TreeNode node = parentQueue.poll();
30+
if(level % 2 == 1) {
31+
subList.add(0,node.val);
32+
} else {
33+
subList.add(node.val);
34+
}
35+
36+
if(node.left != null) {
37+
childrenQueue.add(node.left);
38+
}
39+
if(node.right != null) {
40+
childrenQueue.add(node.right);
41+
}
42+
43+
44+
if(parentQueue.isEmpty()) {
45+
46+
parentQueue = childrenQueue;
47+
childrenQueue= new LinkedList<>();
48+
49+
50+
list.add(subList);
51+
subList = new ArrayList<>();
52+
level ++;
53+
}
54+
55+
}
56+
return list;
57+
58+
}
59+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
class Solution {
3+
4+
//放弃了。。还要用trie.
5+
// 字典树。
6+
Set<String> res = new HashSet<String>();
7+
8+
public List<String> findWords(char[][] board, String[] words) {
9+
Trie trie = new Trie();
10+
for (String word : words) {
11+
trie.insert(word);
12+
}
13+
14+
int m = board.length;
15+
int n = board[0].length;
16+
boolean[][] visited = new boolean[m][n];
17+
for (int i = 0; i < m; i++) {
18+
for (int j = 0; j < n; j++) {
19+
dfs(board, visited, "", i, j, trie);
20+
}
21+
}
22+
23+
return new ArrayList<String>(res);
24+
}
25+
26+
public void dfs(char[][] board, boolean[][] visited, String str, int x, int y, Trie trie) {
27+
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return;
28+
if (visited[x][y]) return;
29+
30+
str += board[x][y];
31+
if (!trie.startsWith(str)) return;
32+
33+
if (trie.search(str)) {
34+
res.add(str);
35+
}
36+
37+
visited[x][y] = true;
38+
dfs(board, visited, str, x - 1, y, trie);
39+
dfs(board, visited, str, x + 1, y, trie);
40+
dfs(board, visited, str, x, y - 1, trie);
41+
dfs(board, visited, str, x, y + 1, trie);
42+
visited[x][y] = false;
43+
}
44+
}

3rd/homework_3/id_80/PathSum.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
13+
// 根节点为null怎么办? 叶子节点为null又怎么办。
14+
// 思路: DFS,没什么特别的吧。
15+
16+
boolean result = false;
17+
public boolean hasPathSum(TreeNode root, int sum) {
18+
if(root == null) {
19+
return false;
20+
}
21+
if (root.left == null && root.right == null) {
22+
return root.val == sum ? true : false;
23+
}
24+
25+
boolean left = hasPathSum(root.left, sum - root.val);
26+
27+
return left == true ? true : hasPathSum(root.right, sum - root.val);
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Definition for singly-linked list.
4+
* class ListNode {
5+
* int val;
6+
* ListNode next;
7+
* ListNode(int x) {
8+
* val = x;
9+
* next = null;
10+
* }
11+
* }
12+
*/
13+
public class Solution {
14+
public ListNode detectCycle(ListNode head) {
15+
/**
16+
* 证明的很漂亮
17+
* Definitions:
18+
Cycle = length of the cycle, if exists.
19+
C is the beginning of Cycle, S is the distance of slow pointer from C when slow pointer meets fast pointer.
20+
21+
Distance(slow) = C + S, Distance(fast) = 2 * Distance(slow) = 2 * (C + S). To let slow poiner meets fast pointer, only if fast pointer run 1 cycle more than slow pointer. Distance(fast) - Distance(slow) = Cycle
22+
=> 2 * (C + S) - (C + S) = Cycle
23+
=> C + S = Cycle
24+
=> C = Cycle - S
25+
=> This means if slow pointer runs (Cycle - S) more, it will reaches C. So at this time, if there's another point2 running from head
26+
=> After C distance, point2 will meet slow pointer at C, where is the beginning of the cycle.
27+
*
28+
*/
29+
30+
31+
32+
ListNode slow = head, fast = head;
33+
while(fast != null && fast.next != null) {
34+
fast = fast.next.next;
35+
slow = slow.next;
36+
if (slow == fast) {
37+
while (head != slow) {
38+
head = head.next;
39+
slow = slow.next;
40+
}
41+
return slow;
42+
}
43+
}
44+
return null;
45+
46+
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
class Solution {
3+
public List<String> generateParenthesis(int n) {
4+
5+
//好像没什么具体思路啊。。如果去穷举?
6+
// 思路1.构建一颗 level = 2*n 的树,每一棵树的左孩子都是左括号,右孩子都是右括号,根节点为左括号。然后dfs遍历,用stack去匹配。匹配成功则塞回到返回值中。可是建树本身无法完成。。。
7+
// 看下别人的思路吧。
8+
// 没能理解这里面的玄机。。。。多想想吧
9+
10+
11+
List<String> list = new ArrayList<String>();
12+
generateOneByOne("", list, n, n);
13+
return list;
14+
}
15+
public void generateOneByOne(String sublist, List<String> list, int left, int right){
16+
if(left > right){
17+
return;
18+
}
19+
if(left > 0){
20+
generateOneByOne( sublist + "(" , list, left-1, right);
21+
}
22+
if(right > 0){
23+
generateOneByOne( sublist + ")" , list, left, right-1);
24+
}
25+
if(left == 0 && right == 0){
26+
list.add(sublist);
27+
return;
28+
}
29+
}
30+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* public class TreeNode {
6+
* int val;
7+
* TreeNode left;
8+
* TreeNode right;
9+
* TreeNode(int x) { val = x; }
10+
* }
11+
*/
12+
class Solution {
13+
public List<Integer> largestValues(TreeNode root) {
14+
// 1.层次遍历找出最大值就好了啊。 用优先级队列去做层次遍历?
15+
// 2。 思路二, DFS?也不是不可以。每次层次DFS,对应的level,去比较大小,最大的被替换。
16+
17+
//来,dfs写一遍。
18+
if (root == null) {
19+
return new ArrayList<>();
20+
}
21+
22+
List<Integer> ret = new ArrayList<>();
23+
findLargestValue(root, ret, 0);
24+
return ret;
25+
26+
}
27+
28+
private void findLargestValue(TreeNode root, List<Integer> ret, int level){
29+
if(ret.size() -1 < level){ //如果是新的一层。怎么处理,待会看
30+
ret.add(root.val);
31+
}
32+
33+
if (root.val > ret.get(level)) {
34+
ret.set(level, root.val);
35+
}
36+
37+
38+
if(root.left != null) {
39+
findLargestValue(root.left, ret, level + 1);
40+
}
41+
if(root.right != null) {
42+
findLargestValue(root.right, ret, level + 1);
43+
}
44+
45+
}
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
}

3rd/homework_7/id_80/NQueens.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
class Solution {
3+
public List<List<String>> solveNQueens(int n) {
4+
5+
// 没有思路,好难。看答案了。
6+
7+
//
8+
9+
10+
char[][] board = new char[n][n];
11+
for(int i = 0; i < n; i++)
12+
for(int j = 0; j < n; j++)
13+
board[i][j] = '.';
14+
List<List<String>> res = new ArrayList<List<String>>();
15+
dfs(board, 0, res);
16+
return res;
17+
}
18+
19+
private void dfs(char[][] board, int colIndex, List<List<String>> res) {
20+
if(colIndex == board.length) {
21+
res.add(construct(board));
22+
return;
23+
}
24+
25+
for(int i = 0; i < board.length; i++) {
26+
if(validate(board, i, colIndex)) {
27+
board[i][colIndex] = 'Q';
28+
dfs(board, colIndex + 1, res);
29+
board[i][colIndex] = '.';
30+
}
31+
}
32+
}
33+
34+
private boolean validate(char[][] board, int x, int y) {
35+
for(int i = 0; i < board.length; i++) {
36+
for(int j = 0; j < y; j++) {
37+
if(board[i][j] == 'Q' && (x + j == y + i || x + y == i + j || x == i)) //为什么不判断y == j?
38+
return false;
39+
}
40+
}
41+
42+
return true;
43+
}
44+
45+
private List<String> construct(char[][] board) {
46+
List<String> res = new LinkedList<String>();
47+
for(int i = 0; i < board.length; i++) {
48+
String s = new String(board[i]);
49+
res.add(s);
50+
}
51+
return res;
52+
}
53+
54+
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
4+
class Solution {
5+
6+
7+
// 应该创建一个新数组返回,对于矩阵中的每一个元素,去做BFS/DFS的check操作。
8+
9+
10+
// 方法写出来了,可是StackOverflowError了。
11+
//在想,是不是应该 遍历的时候,先从0开始刷。以0位中心,把周围刷完?
12+
public int[][] updateMatrix(int[][] matrix) {
13+
int m = matrix.length;
14+
int n = matrix[0].length;
15+
16+
Queue<int[]> queue = new LinkedList<>();
17+
for (int i = 0; i < m; i++) {
18+
for (int j = 0; j < n; j++) {
19+
if (matrix[i][j] == 0) {
20+
queue.offer(new int[] {i, j});
21+
}
22+
else {
23+
matrix[i][j] = Integer.MAX_VALUE;
24+
}
25+
}
26+
}
27+
28+
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
29+
30+
while (!queue.isEmpty()) {
31+
int[] cell = queue.poll();
32+
for (int[] d : dirs) {
33+
int r = cell[0] + d[0];
34+
int c = cell[1] + d[1];
35+
if (r < 0 || r >= m || c < 0 || c >= n ||
36+
matrix[r][c] <= matrix[cell[0]][cell[1]] + 1) continue;
37+
queue.add(new int[] {r, c});
38+
matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
39+
}
40+
}
41+
42+
return matrix;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)