Skip to content

Commit 145d30a

Browse files
committed
2020-4-15
1 parent 7362eda commit 145d30a

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package lcci0305_sort_of_stacks;
2+
3+
public class SortedStack {
4+
5+
public SortedStack() {
6+
7+
}
8+
9+
public void push(int val) {
10+
11+
}
12+
13+
public void pop() {
14+
15+
}
16+
17+
public int peek() {
18+
return -1;
19+
}
20+
21+
public boolean isEmpty() {
22+
return false;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package lcci0402_minimum_height_tree;
2+
3+
/**
4+
* 执行用时:0ms,击败100.00%。消耗内存:39.4MB,击败100.00%。
5+
*/
6+
public class Solution {
7+
public TreeNode sortedArrayToBST(int[] nums) {
8+
return sortedArrayToBST(nums, 0, nums.length - 1);
9+
}
10+
11+
private TreeNode sortedArrayToBST(int[] nums, int left, int right) {
12+
if (left > right) {
13+
return null;
14+
}
15+
if (left == right) {
16+
return new TreeNode(nums[left]);
17+
}
18+
int mid = left + ((right - left) >> 1);
19+
TreeNode treeNode = new TreeNode(nums[mid]);
20+
treeNode.left = sortedArrayToBST(nums, left, mid - 1);
21+
treeNode.right = sortedArrayToBST(nums, mid + 1, right);
22+
return treeNode;
23+
}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lcci0402_minimum_height_tree;
2+
3+
public class TreeNode {
4+
int val;
5+
6+
TreeNode left;
7+
8+
TreeNode right;
9+
10+
TreeNode(int x) {
11+
val = x;
12+
}
13+
}

lcci0404_check_balance/Solution.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lcci0404_check_balance;
2+
3+
/**
4+
* 执行用时:1ms,击败100.00%。消耗内存:39.8MB,击败100.00%。
5+
*/
6+
public class Solution {
7+
public boolean isBalanced(TreeNode root) {
8+
if (null == root) {
9+
return true;
10+
}
11+
if (Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) {
12+
return false;
13+
}
14+
return isBalanced(root.left) && isBalanced(root.right);
15+
}
16+
17+
private int getHeight(TreeNode treeNode) {
18+
if (null == treeNode) {
19+
return 0;
20+
}
21+
return 1 + Math.max(getHeight(treeNode.left), getHeight(treeNode.right));
22+
}
23+
}

lcci0404_check_balance/TreeNode.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lcci0404_check_balance;
2+
3+
public class TreeNode {
4+
int val;
5+
6+
TreeNode left;
7+
8+
TreeNode right;
9+
10+
TreeNode(int x) {
11+
val = x;
12+
}
13+
}

question0542_01_matrix/Solution.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package question0542_01_matrix;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* 广搜。
8+
*
9+
* 时间复杂度和空间复杂度均是O(m * n)。
10+
*
11+
* 执行用时:18ms,击败58.44%。消耗内存:42.1MB,击败100.00%。
12+
*/
13+
public class Solution {
14+
public int[][] updateMatrix(int[][] matrix) {
15+
int m;
16+
if (null == matrix || (m = matrix.length) == 0) {
17+
return new int[0][0];
18+
}
19+
int n;
20+
if (null == matrix[0] || (n = matrix[0].length) == 0) {
21+
return new int[0][0];
22+
}
23+
int[][] result = new int[m][n], directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
24+
Queue<Integer> queue = new LinkedList<>();
25+
for (int i = 0; i < m; i++) {
26+
for (int j = 0; j < n; j++) {
27+
if (matrix[i][j] == 0) {
28+
matrix[i][j] = -1;
29+
queue.add(i * n + j);
30+
}
31+
}
32+
}
33+
int distance = 0;
34+
while (!queue.isEmpty()) {
35+
int qSize = queue.size();
36+
for (int i = 0; i < qSize; i++) {
37+
int now = queue.poll(), nowX = now / n, nowY = now % n;
38+
result[nowX][nowY] = distance;
39+
for (int j = 0; j < 4; j++) {
40+
int newX = nowX + directions[j][0], newY = nowY + directions[j][1];
41+
if (newX >= 0 && newX < m && newY >= 0 && newY < n && matrix[newX][newY] != -1) {
42+
matrix[newX][newY] = -1;
43+
queue.add(newX * n + newY);
44+
}
45+
}
46+
}
47+
distance++;
48+
}
49+
return result;
50+
}
51+
}

0 commit comments

Comments
 (0)