Skip to content

Commit 2aef401

Browse files
Merge pull request #93 from HuizeHuang/master
第一周作业-#48
2 parents f7ada31 + 27ae3ea commit 2aef401

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

Week_01/id_48/LeetCode_236_48.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
2+
//
3+
// According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
4+
//
5+
// Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
6+
//
7+
//
8+
//
9+
// Example 1:
10+
//
11+
//
12+
//Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
13+
//Output: 3
14+
//Explanation: The LCA of nodes 5 and 1 is 3.
15+
//
16+
//
17+
// Example 2:
18+
//
19+
//
20+
//Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
21+
//Output: 5
22+
//Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
23+
//
24+
//
25+
//
26+
//
27+
// Note:
28+
//
29+
//
30+
// All of the nodes' values will be unique.
31+
// p and q are different and both values will exist in the binary tree.
32+
//
33+
//
34+
35+
import javax.swing.tree.TreeNode;
36+
37+
/**
38+
* Definition for a binary tree node.
39+
* public class TreeNode {
40+
* int val;
41+
* TreeNode left;
42+
* TreeNode right;
43+
* TreeNode(int x) { val = x; }
44+
* }
45+
*/
46+
class Solution {
47+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
48+
/**
49+
* 摘自解答:
50+
注意p,q必然存在树内, 且所有节点的值唯一!!!
51+
递归思想, 对以root为根的(子)树进行查找p和q, 如果root == null || p || q 直接返回root
52+
表示对于当前树的查找已经完毕, 否则对左右子树进行查找, 根据左右子树的返回值判断:
53+
1. 左右子树的返回值都不为null, 由于值唯一左右子树的返回值就是p和q, 此时root为LCA
54+
2. 如果左右子树返回值只有一个不为null, 说明只有p和q存在于左或右子树中, 最先找到的那个节点为LCA
55+
3. 左右子树返回值均为null, p和q均不在树中, 返回null
56+
**/
57+
if (root == null || root == p || root == q) return root;
58+
TreeNode left = lowestCommonAncestor(root.left, p, q);
59+
TreeNode right = lowestCommonAncestor(root.right, p, q);
60+
if (left != null && right != null) return root;
61+
else if (left == null && right == null) return null;
62+
else return left == null ? right : left;
63+
64+
}
65+
}

Week_01/id_48/LeetCode_26_48.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
2+
//
3+
// Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
4+
//
5+
// Example 1:
6+
//
7+
//
8+
//Given nums = [1,1,2],
9+
//
10+
//Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
11+
//
12+
//It doesn't matter what you leave beyond the returned length.
13+
//
14+
// Example 2:
15+
//
16+
//
17+
//Given nums = [0,0,1,1,1,2,2,3,3,4],
18+
//
19+
//Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
20+
//
21+
//It doesn't matter what values are set beyond the returned length.
22+
//
23+
//
24+
// Clarification:
25+
//
26+
// Confused why the returned value is an integer but your answer is an array?
27+
//
28+
// Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
29+
//
30+
// Internally you can think of this:
31+
//
32+
//
33+
//// nums is passed in by reference. (i.e., without making a copy)
34+
//int len = removeDuplicates(nums);
35+
//
36+
//// any modification to nums in your function would be known by the caller.
37+
//// using the length returned by your function, it prints the first len elements.
38+
//for (int i = 0; i < len; i++) {
39+
//    print(nums[i]);
40+
//}
41+
//
42+
43+
class Solution {
44+
public int removeDuplicates(int[] nums) {
45+
// two pointers
46+
if (nums == null || nums.length == 0) return 0;
47+
// i is the last non-duplicated digit, j is fast pointer
48+
int i = 0;
49+
for (int j = 1; j < nums.length; j++){
50+
if (nums[j] != nums[i]) {
51+
nums[++i] = nums[j];
52+
}
53+
}
54+
return i + 1;
55+
}
56+
}

Week_01/id_48/MindMap.jpg

998 KB
Loading

0 commit comments

Comments
 (0)