Skip to content

Commit 7abeb64

Browse files
author
guoshufeng
committed
gsf 第三周
1 parent 5e94bd6 commit 7abeb64

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

Week_03/id_30/LeetCode_104_30.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.shufeng.algorithm.d0_;
2+
3+
/**
4+
* @author gsf
5+
*/
6+
public class LeetCode_104_30 {
7+
8+
public static void main(String[] args) {
9+
TreeNode root = new TreeNode(3);
10+
root.left = new TreeNode(9);
11+
root.right = new TreeNode(20);
12+
root.right.left = new TreeNode(15);
13+
root.right.right = new TreeNode(7);
14+
int i = maxDepth(root);
15+
System.out.println(i);
16+
}
17+
18+
public static int maxDepth(TreeNode root) {
19+
if (root == null) {
20+
return 0;
21+
}
22+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
23+
}
24+
}
25+

Week_03/id_30/LeetCode_429_30.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.shufeng.algorithm.d0_;
2+
3+
/**
4+
* @author gsf
5+
*/
6+
public class LeetCode_429_30 {
7+
8+
public static void main(String[] args) {
9+
10+
Node root = new Node(1, Arrays.asList(
11+
new Node(3, Arrays.asList(
12+
new Node(5, null),
13+
new Node(6, null))),
14+
new Node(2, null),
15+
new Node(4, null)
16+
));
17+
List<List<Integer>> lists = levelOrder(root);
18+
System.out.println(lists);
19+
}
20+
21+
public static List<List<Integer>> levelOrder(Node root) {
22+
List<List<Integer>> lists = new ArrayList<>();
23+
lists.add(new ArrayList<>());
24+
if (root == null) {
25+
return lists;
26+
}
27+
28+
int num = 0;
29+
lists.get(num).add(root.val);
30+
levelOrder(root.children, lists, num + 1);
31+
return lists;
32+
}
33+
34+
public static void levelOrder(List<Node> children, List<List<Integer>> lists, int num) {
35+
if (children == null) {
36+
return;
37+
}
38+
for (int i = 0; i < children.size(); i++) {
39+
Node node = children.get(i);
40+
if (lists.size() < num + 1) {
41+
lists.add(new ArrayList<>());
42+
}
43+
lists.get(num).add(node.val);
44+
levelOrder(node.children, lists, num + 1);
45+
}
46+
}
47+
}
48+

Week_03/id_30/LeetCode_703_30.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.shufeng.algorithm.d0_;
2+
3+
/**
4+
* @author gsf
5+
*/
6+
public class LeetCode_703_30 {
7+
8+
public static void main(String[] args) {
9+
10+
int k = 3;
11+
int[] arr = {4, 5, 8, 2};
12+
D703 kthLargest = new D703(k, arr);
13+
System.out.println(kthLargest.add(3)); // returns 4
14+
System.out.println(kthLargest.add(5)); // returns 5
15+
System.out.println(kthLargest.add(10)); // returns 5
16+
System.out.println(kthLargest.add(9)); // returns 8
17+
System.out.println(kthLargest.add(4)); // returns 8
18+
19+
}
20+
21+
PriorityQueue<Integer> queue = new PriorityQueue<>();
22+
int num = 0;
23+
24+
public D703(int k, int[] nums) {
25+
num = k;
26+
for (int i = 0; i < nums.length; i++) {
27+
if (queue.size() == k) {
28+
Integer peek = queue.peek();
29+
if (peek < nums[i]) {
30+
queue.poll();
31+
queue.add(nums[i]);
32+
}
33+
} else {
34+
queue.add(nums[i]);
35+
}
36+
}
37+
}
38+
39+
public int add(int val) {
40+
if (queue.size() == num) {
41+
Integer peek = queue.peek();
42+
if (peek < val) {
43+
queue.poll();
44+
queue.add(val);
45+
return queue.peek();
46+
}
47+
} else {
48+
queue.add(val);
49+
}
50+
return queue.peek();
51+
}
52+
}
53+

Week_03/id_30/LeetCode_997_30.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.shufeng.algorithm.d0_;
2+
3+
/**
4+
* @author gsf
5+
*/
6+
public class LeetCode_997_30 {
7+
8+
public static void main(String[] args) {
9+
int[][] trust = new int[2][2];
10+
int[] a = {1, 3};
11+
int[] a1 = {2, 3};
12+
13+
trust[0] = a;
14+
trust[1] = a1;
15+
16+
17+
int judge = findJudge(1, trust);
18+
System.out.println(judge);
19+
}
20+
21+
public static int findJudge(int N, int[][] trust) {
22+
int[] arr = new int[N + 1];
23+
Set<Integer> set = new HashSet<>();
24+
for (int i = 0; i < trust.length; i++) {
25+
if (trust[i].length > 0) {
26+
arr[trust[i][1]]++;
27+
set.add(trust[i][0]);
28+
}
29+
}
30+
31+
if (set.size() != N - 1) {
32+
System.out.println(set.size());
33+
return -1;
34+
}
35+
for (int i = 1; i < arr.length; i++) {
36+
if (arr[i] == N - 1) {
37+
return i;
38+
}
39+
}
40+
return -1;
41+
}
42+
}
43+

0 commit comments

Comments
 (0)