forked from algorithm008-class02/algorithm008-class02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass2Week2.java
More file actions
144 lines (116 loc) · 3.33 KB
/
Class2Week2.java
File metadata and controls
144 lines (116 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.*;
public class Class2Week2 {
//两数之和
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int key = target - nums[i];
if (map.containsKey(key)) {
return new int[]{map.get(key), i};
}
map.put(nums[i], i);
}
return null;
}
//242. 有效的字母异位词
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
return false;
}
//94. 二叉树的中序遍历
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || stack.size() > 0) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
TreeNode pop = stack.pop();
res.add(pop.val);
cur = cur.right;
}
return res;
}
//144. 二叉树的前序遍历
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || stack.size() > 0) {
while (cur != null) {
stack.push(cur);
res.add(cur.val);
cur = cur.left;
}
TreeNode pop = stack.pop();
cur = pop.right;
}
return res;
}
//589. N叉树的前序遍历
public List<Integer> preorder(Node root) {
List<Integer> res = new ArrayList<>();
Stack<Node> stack = new Stack<>();
Node cur = root;
stack.push(root);
while (stack != null) {
Node pop = stack.pop();
res.add(pop.val);
Collections.reverse(pop.children);
for (Node child : pop.children) {
stack.push(child);
}
}
return res;
}
public List<Integer> topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int num : nums) {
if (hashMap.containsKey(num)) {
hashMap.put(num, hashMap.get(num) + 1);
} else {
hashMap.put(num, 1);
}
}
PriorityQueue<Integer> heap = new PriorityQueue<Integer>((x, y) -> hashMap.get(x) - hashMap.get(y));
Set<Integer> set = hashMap.keySet();
for (Integer n : set) {
heap.add(n);
if (heap.size() > k) {
heap.poll();
}
}
List<Integer> topk = new LinkedList();
while (!heap.isEmpty()) {
topk.add(heap.poll());
}
Collections.reverse(topk);
return topk;
}
}
//347. 前 K 个高频元素
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Node {
public int val;
public List<Node> children;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
}