Skip to content

Commit 7fc473e

Browse files
committed
clean
1 parent f710b64 commit 7fc473e

26 files changed

+3236
-1966
lines changed

Java/Backspace String Compare.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
E
2+
1533516441
3+
tags: Two Pointers, Stack
4+
5+
```
6+
/*
7+
Given two strings S and T, return if they are equal when both are typed into empty text editors.
8+
# means a backspace character.
9+
10+
Example 1:
11+
12+
Input: S = "ab#c", T = "ad#c"
13+
Output: true
14+
Explanation: Both S and T become "ac".
15+
Example 2:
16+
17+
Input: S = "ab##", T = "c#d#"
18+
Output: true
19+
Explanation: Both S and T become "".
20+
Example 3:
21+
22+
Input: S = "a##c", T = "#a#c"
23+
Output: true
24+
Explanation: Both S and T become "c".
25+
Example 4:
26+
27+
Input: S = "a#c", T = "b"
28+
Output: false
29+
Explanation: S becomes "c" while T becomes "b".
30+
Note:
31+
32+
1 <= S.length <= 200
33+
1 <= T.length <= 200
34+
S and T only contain lowercase letters and '#' characters.
35+
Follow up:
36+
37+
Can you solve it in O(N) time and O(1) space?
38+
39+
*/
40+
41+
/*
42+
Stack to take elements, and pop when # if reached.
43+
compare the two stacks at the end.
44+
time/space: O(n)
45+
*/
46+
class Solution {
47+
public boolean backspaceCompare(String S, String T) {
48+
if (S == null || T == null) return false;
49+
50+
return type(S).equals(type(T));
51+
}
52+
53+
private String type(String s) {
54+
Stack<Character> stack = new Stack<>();
55+
for (char c : s.toCharArray()) {
56+
if (c == '#') {
57+
if (!stack.isEmpty()) stack.pop();
58+
} else {
59+
stack.push(c);
60+
}
61+
}
62+
StringBuffer sb = new StringBuffer();
63+
while (!stack.isEmpty()) {
64+
sb.append(stack.pop());
65+
}
66+
return sb.toString();
67+
}
68+
}
69+
```

Java/Diameter of Binary Tree.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
E
2+
1533515503
3+
tags: Tree
4+
5+
找longest path (include or not include root)
6+
7+
跟Binary Tree Maximum Path Sum 的想法一样: 处理single path, 或者combined path (do not include curr root)
8+
9+
#### Singlepath, combined path
10+
- `int[]{combinedPath, singlePath}`;
11+
- pick single path + 1: `singlePath = Math.max(left[1] , right[1]) + 1`;
12+
- complete left/right child, or join curr root: `combinedPath = Math.max(Math.max(left[0], right[0]), left[1] + right[1] + 1)`;
13+
14+
```
15+
/*
16+
Given a binary tree, you need to compute the length of the diameter of the tree.
17+
The diameter of a binary tree is the length of the longest path between any two nodes in a tree.
18+
This path may or may not pass through the root.
19+
20+
Example:
21+
Given a binary tree
22+
1
23+
/ \
24+
2 3
25+
/ \
26+
4 5
27+
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
28+
29+
Note: The length of path between two nodes is represented by the number of edges between them.
30+
31+
32+
*/
33+
34+
/*
35+
No special condition: left+root+right, or left, or right
36+
recursive on itself.
37+
end condition: if null -> 0, if leaf -> 1
38+
*/
39+
class Solution {
40+
public int diameterOfBinaryTree(TreeNode root) {
41+
if (root == null) return 0;
42+
int[] rst = dfs(root);
43+
44+
return Math.max(rst[0], rst[1]) - 1;
45+
}
46+
47+
private int[] dfs(TreeNode root) {
48+
if (root == null) return new int[] {0, 0};
49+
50+
int[] left = dfs(root.left);
51+
int[] right = dfs(root.right);
52+
int singlePath = Math.max(left[1] , right[1]) + 1; // pick single path + 1
53+
int combinedPath = Math.max(Math.max(left[0], right[0]), left[1] + right[1] + 1); // complete left/right child, or join curr root.
54+
return new int[]{combinedPath, singlePath};
55+
}
56+
}
57+
```

Java/Flood Fill.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
E
2+
1533512047
3+
tags: DFS
4+
5+
Same as MS Paint
6+
7+
#### DFS
8+
- track `boolean[][] visited`, validate before dfs
9+
10+
```
11+
/*
12+
An image is represented by a 2-D array of integers,
13+
each integer representing the pixel value of the image (from 0 to 65535).
14+
15+
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill,
16+
and a pixel value newColor, "flood fill" the image.
17+
18+
To perform a "flood fill", consider the starting pixel,
19+
plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel,
20+
plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel),
21+
and so on. Replace the color of all of the aforementioned pixels with the newColor.
22+
23+
At the end, return the modified image.
24+
25+
Example 1:
26+
Input:
27+
image = [[1,1,1],[1,1,0],[1,0,1]]
28+
sr = 1, sc = 1, newColor = 2
29+
Output: [[2,2,2],[2,2,0],[2,0,1]]
30+
Explanation:
31+
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
32+
by a path of the same color as the starting pixel are colored with the new color.
33+
Note the bottom corner is not colored 2, because it is not 4-directionally connected
34+
to the starting pixel.
35+
Note:
36+
37+
The length of image and image[0] will be in the range [1, 50].
38+
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
39+
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
40+
41+
*/
42+
43+
/*
44+
dfs, then update curr color.
45+
mark boolean visited[][]
46+
*/
47+
class Solution {
48+
int[] dx = {1, -1, 0, 0};
49+
int[] dy = {0, 0, 1, -1};
50+
boolean[][] visited;
51+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
52+
int m = image.length, n = image[0].length;
53+
visited = new boolean[m][n];
54+
dfs(image, sr, sc, image[sr][sc], newColor);
55+
return image;
56+
}
57+
58+
private void dfs(int[][] image, int sr, int sc, int original, int newColor) {
59+
image[sr][sc] = newColor;
60+
visited[sr][sc] = true;
61+
62+
for (int i = 0; i < 4; i++) {
63+
int x = sr + dx[i], y = sc + dy[i];
64+
if (validate(image, x, y, original)) {
65+
dfs(image, x, y, original, newColor);
66+
}
67+
}
68+
}
69+
70+
private boolean validate(int[][] image, int x, int y, int original) {
71+
int m = image.length, n = image[0].length;
72+
return x >= 0 && x < m && y >= 0 && y < n && image[x][y] == original && !visited[x][y];
73+
}
74+
}
75+
```

Java/Intersection of Two Linked Lists.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
E
2-
tags: Linked List
32
1525664839
3+
tags: Linked List
44

55
给两个 linked list, 问从哪个node开始, 两个 linked list 开始有重复?
66

Java/Move Zeroes.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
E
2+
1533511284
3+
tags: Array, Two Pointers
4+
5+
Move non-zero elements to front of array; preseve order.
6+
7+
#### Two Pointers
8+
- Outside pointer that moves in certain condition.
9+
- Save appropirate elements
10+
11+
```
12+
/*
13+
Given an array nums, write a function to move all 0's to the end of it
14+
while maintaining the relative order of the non-zero elements.
15+
16+
Example:
17+
18+
Input: [0,1,0,3,12]
19+
Output: [1,3,12,0,0]
20+
Note:
21+
22+
You must do this in-place without making a copy of the array.
23+
Minimize the total number of operations.
24+
*/
25+
/*
26+
- Use pointer to move all elements to non-zero index.
27+
- set remaining list -> 0
28+
*/
29+
class Solution {
30+
public void moveZeroes(int[] nums) {
31+
if (nums == null || nums.length == 0) return;
32+
int index = 0;
33+
for (int i = 0; i < nums.length; i++) {
34+
if (nums[i] != 0) {
35+
nums[index++] = nums[i];
36+
}
37+
}
38+
for (int i = index; i < nums.length; i++) {
39+
nums[i] = 0;
40+
}
41+
}
42+
}
43+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
E
2+
1533510925
3+
tags:Design, Queue
4+
5+
给一个interface, design一个structure, 能够计算moving window average.
6+
7+
#### Queue
8+
- 读懂题目, 注意average window 的处理.
9+
- 简单的queue.size() comparison
10+
11+
```
12+
/**
13+
Given a stream of integers and a window size,
14+
calculate the moving average of all integers in the sliding window.
15+
16+
For example,
17+
MovingAverage m = new MovingAverage(3);
18+
m.next(1) = 1
19+
m.next(10) = (1 + 10) / 2
20+
m.next(3) = (1 + 10 + 3) / 3
21+
m.next(5) = (10 + 3 + 5) / 3
22+
*/
23+
24+
class MovingAverage {
25+
double sum;
26+
int size;
27+
Queue<Integer> queue;
28+
29+
/** Initialize your data structure here. */
30+
public MovingAverage(int size) {
31+
this.sum = 0.0;
32+
this.size = size;
33+
this.queue = new LinkedList<>();
34+
}
35+
36+
public double next(int val) {
37+
sum += val;
38+
queue.offer(val);
39+
if (queue.size() > size) {
40+
sum -= queue.poll();
41+
}
42+
return sum / queue.size();
43+
}
44+
}
45+
46+
/**
47+
* Your MovingAverage object will be instantiated and called as such:
48+
* MovingAverage obj = new MovingAverage(size);
49+
* double param_1 = obj.next(val);
50+
*/
51+
```

0 commit comments

Comments
 (0)