Skip to content

Commit cecd659

Browse files
Merge pull request #33 from hiColors/master
第一周作业#9
2 parents 1ff4223 + b0bd2fa commit cecd659

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

Week_01/id_9/LeetCode_104_9.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.lifelab.leetcode.problemset;
2+
3+
/**
4+
* 二叉树的最大深度 @https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
5+
*
6+
* @author Weichao Li ([email protected])
7+
* @since 2019-06-03
8+
*/
9+
public class Solution104 {
10+
11+
public int maxDepth(TreeNode root) {
12+
return depth(root, 0);
13+
}
14+
15+
private int depth(TreeNode node, int depth) {
16+
17+
if (node == null) {
18+
return 0;
19+
}
20+
21+
depth++;
22+
23+
if (node.left == null && node.right == null) {
24+
return depth;
25+
}
26+
27+
int l = 0, r = 0;
28+
29+
if (node.left != null) {
30+
l = depth(node.left, depth);
31+
}
32+
33+
if (node.right != null) {
34+
r = depth(node.right, depth);
35+
}
36+
37+
return Math.max(l, r);
38+
}
39+
}

Week_01/id_9/LeetCode_236_9.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.lifelab.leetcode.problemset;
2+
3+
/**
4+
* 二叉树的最近公共祖先 @https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/
5+
*
6+
* @author Weichao Li ([email protected])
7+
* @since 2019-06-09
8+
*/
9+
public class Solution236 {
10+
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
13+
if (root == null) {
14+
return null;
15+
}
16+
17+
if (root.val == p.val || root.val == q.val) {
18+
return root;
19+
}
20+
21+
TreeNode leftVal = lowestCommonAncestor(root.left, p, q);
22+
23+
TreeNode rightVal = lowestCommonAncestor(root.right, p, q);
24+
25+
if (rightVal != null && leftVal != null) {
26+
return root;
27+
}
28+
29+
if (rightVal != null) {
30+
return rightVal;
31+
}
32+
33+
if (leftVal != null) {
34+
return leftVal;
35+
}
36+
return null;
37+
}
38+
39+
}

Week_01/id_9/LeetCode_26_9.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.lifelab.leetcode.problemset;
2+
3+
/**
4+
* 删除排序数组中的重复项 @https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
5+
*
6+
* @author weichao.li ([email protected])
7+
* @since 2019-06-04
8+
*/
9+
public class Solution26 {
10+
11+
/**
12+
* 1 1 1 2 3 4 4 4
13+
*
14+
* @param nums
15+
* @return
16+
* @
17+
*/
18+
public int removeDuplicates(int[] nums) {
19+
if (nums == null || nums.length == 0) {
20+
return 0;
21+
}
22+
int left = 0;
23+
for (int i = 1; i < nums.length; i++) {
24+
if (nums[left] != nums[i]) {
25+
left++;
26+
nums[left] = nums[i];
27+
}
28+
}
29+
return left + 1;
30+
}
31+
32+
33+
}

Week_01/id_9/LeetCode_50_9.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.lifelab.leetcode.problemset;
2+
3+
/**
4+
* Pow(x, n) @https://leetcode-cn.com/problems/powx-n/
5+
*
6+
* @author Weichao Li ([email protected])
7+
* @since 2019-06-06
8+
*/
9+
public class Solution50 {
10+
11+
public double myPow(double x, int n) {
12+
13+
if (n < 0) {
14+
return 1 / halfPow(x, -n);
15+
} else {
16+
return halfPow(x, n);
17+
}
18+
19+
}
20+
21+
public double halfPow(double x, int n) {
22+
23+
//terminator
24+
if (n == 0) {
25+
return 1;
26+
}
27+
if (n == 1) {
28+
return x;
29+
}
30+
//process && drill down
31+
32+
double result;
33+
34+
boolean flag = n % 2 == 0;
35+
36+
double temp = halfPow(x, n / 2);
37+
38+
if (flag) {
39+
result = temp * temp;
40+
} else {
41+
result = temp * temp * x;
42+
}
43+
44+
return result;
45+
}
46+
}

Week_01/id_9/NOTE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# 学习笔记
2+
3+
> 在 LeetCode 刷题的过程中,可对部分题进行变形简化处理逻辑。
Binary file not shown.

0 commit comments

Comments
 (0)