Skip to content

Commit f6e8d65

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent f1a6b95 commit f6e8d65

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@
385385

386386
[220. Contains Duplicate III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/220.%20Contains%20Duplicate%20III.md)
387387

388+
[221. Maximal Square](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/221.%20Maximal%20Square.md)
389+
390+
[222. Count Complete Tree Nodes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/222.%20Count%20Complete%20Tree%20Nodes.md)
391+
392+
[223. Rectangle Area](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/223.%20Rectangle%20Area.md)
393+
388394
## Algorithm(4th_Edition)
389395
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
390396
All java realization codes are placed in different packages.

leetcode/221. Maximal Square.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,32 @@ class Solution {
5858
return result * result;
5959
}
6060
}
61+
```
62+
63+
### 二刷
64+
1. 原来还是想要用dp来做,用每一个位置存储面积,实际上应该存储边长更为合适。
65+
2. 参考了一刷,如果左,上,左上为0,则当前点为0,不然取出三个值中的最小值 + 1。
66+
```Java
67+
class Solution {
68+
public int maximalSquare(char[][] matrix) {
69+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
70+
int height = matrix.length, width = matrix[0].length;
71+
int max = 0;
72+
int[][] dp = new int[height][width];
73+
for(int i = 0; i < height; i++){
74+
for(int j = 0; j < width; j++){
75+
if(matrix[i][j] == '1'){
76+
if(i > 0 && j > 0){
77+
if(dp[i - 1][j] == 0 || dp[i][j - 1] == 0 || dp[i - 1][j - 1] == 0)
78+
dp[i][j] = 1;
79+
else
80+
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
81+
}else dp[i][j] = 1;
82+
}
83+
max = Math.max(max, dp[i][j]);
84+
}
85+
}
86+
return max * max;
87+
}
88+
}
6189
```

leetcode/222. Count Complete Tree Nodes.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,50 @@ class Solution {
105105
return rightHeight(root.right) + 1;
106106
}
107107
}
108+
```
109+
110+
### 二刷
111+
1. DFS
112+
```Java
113+
/**
114+
* Definition for a binary tree node.
115+
* public class TreeNode {
116+
* int val;
117+
* TreeNode left;
118+
* TreeNode right;
119+
* TreeNode(int x) { val = x; }
120+
* }
121+
*/
122+
class Solution {
123+
int count = 0;
124+
public int countNodes(TreeNode root) {
125+
dfs(root);
126+
return this.count;
127+
}
128+
private void dfs(TreeNode node){
129+
if(node == null) return;
130+
count ++;
131+
if(node.left != null) dfs(node.left);
132+
if(node.right != null) dfs(node.right);
133+
}
134+
}
135+
```
136+
137+
2. 仍是使用DFS,不使用额外的成员变量。
138+
```Java
139+
/**
140+
* Definition for a binary tree node.
141+
* public class TreeNode {
142+
* int val;
143+
* TreeNode left;
144+
* TreeNode right;
145+
* TreeNode(int x) { val = x; }
146+
* }
147+
*/
148+
class Solution {
149+
public int countNodes(TreeNode root) {
150+
if(root == null) return 0;
151+
return countNodes(root.left) + countNodes(root.right) + 1;
152+
}
153+
}
108154
```

leetcode/223. Rectangle Area.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@ class Solution {
2828
}
2929
}
3030
}
31+
```
32+
33+
### 二刷
34+
1. 和一刷的想法一样,但是出了一个问题,两个长方形的相对位置是不确定的,所以我们要使用最大值,最小值来解决这个问题。
35+
```Java
36+
class Solution {
37+
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
38+
int total = (D - B) * (C - A) + (H - F) * (G - E);
39+
if(E >= C || G <= A || H <= B || F >= D) return total;
40+
return total - (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(F, B));
41+
}
42+
}
3143
```

0 commit comments

Comments
 (0)