Skip to content

Commit 08c4f32

Browse files
committed
Week2 homework in Algorithm Class 10
1 parent aba8f02 commit 08c4f32

6 files changed

Lines changed: 175 additions & 1 deletion
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> preorderTraversal(TreeNode* root) {
4+
vector<int> res;
5+
if (!root) return res;
6+
7+
stack<TreeNode*> s;
8+
s.push(root);
9+
while(s.size()) {
10+
TreeNode * p = s.top();
11+
s.pop();
12+
if (p) res.push_back(p->val);
13+
if(p->right) s.push(p->right);
14+
if(p->left) s.push(p->left);
15+
}
16+
17+
return res;
18+
}
19+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public:
3+
vector<int> topKFrequent(vector<int>& nums, int k) {
4+
unordered_map<int, int> frep;
5+
for (int i : nums) frep[i]++;
6+
7+
auto cmp = [](pair<int, int>& a, pair<int, int>& b) {return a.second > b.second;};
8+
priority_queue<pair<int, int> , vector<pair<int, int>>, decltype(cmp)> pq(cmp);
9+
10+
for (auto f : frep) {
11+
pq.push(f);
12+
if (pq.size() > k) pq.pop();
13+
}
14+
15+
vector<int> res;
16+
for (int i = 0; i < k; i++) {
17+
res.push_back(pq.top().first);
18+
pq.pop();
19+
}
20+
21+
return res;
22+
}
23+
24+
// vector<int> topKFrequent(vector<int>& nums, int k) {
25+
// unordered_map<int, int> frep;
26+
// for (int i : nums) frep[i]++;
27+
28+
// //超长buckets
29+
// vector<vector<int>> buckets(nums.size() + 1);
30+
31+
// for (auto f : frep)
32+
// buckets.at(f.second).push_back(f.first);
33+
34+
// vector<int> res;
35+
// for (int i = buckets.size() - 1; i >= 0; i--) {
36+
// for (auto bucket : buckets[i]) {
37+
// res.push_back(bucket);
38+
// if (res.size() == k) return res;
39+
// }
40+
// }
41+
42+
// return res;
43+
// }
44+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
vector<Node*> children;
7+
8+
Node() {}
9+
10+
Node(int _val) {
11+
val = _val;
12+
}
13+
14+
Node(int _val, vector<Node*> _children) {
15+
val = _val;
16+
children = _children;
17+
}
18+
};
19+
*/
20+
21+
class Solution {
22+
public:
23+
vector<vector<int>> levelOrder(Node* root) {
24+
if (!root) return {};
25+
26+
vector<vector<int>> res;
27+
queue<Node*> q;
28+
q.push(root);
29+
q.push(NULL);
30+
vector<int> line;
31+
32+
while(q.size()) {
33+
Node * p = q.front();
34+
q.pop();
35+
36+
if (p) {
37+
line.push_back(p->val);
38+
for (auto cl : p->children)
39+
if (cl) q.push(cl);
40+
} else {
41+
res.push_back(line);
42+
line.clear();
43+
if (q.size()) q.push(NULL);
44+
}
45+
}
46+
return res;
47+
}
48+
};

Week02/LeetCodes/49.UglyNum.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int nthUglyNumber(int n) {
4+
vector<int> ug(n, 1);
5+
6+
int c2 = 2, c3 = 3, c5 = 5;
7+
int p2 = 0, p3 = 0, p5 = 0;
8+
for(int i = 1; i < n; i++) {
9+
int t = ug[i] = min(min(c2, c3), c5);
10+
if (c2 == t) c2 = ug[++p2] * 2;
11+
if (c3 == t) c3 = ug[++p3] * 3;
12+
if (c5 == t) c5 = ug[++p5] * 5;
13+
}
14+
return ug[n-1];
15+
16+
}
17+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
enum {Black=0, White};
4+
vector<int> inorderTraversal(TreeNode* root) {
5+
if (!root) return {};
6+
7+
stack<pair<TreeNode*, int>> s;
8+
s.push({root, Black});
9+
10+
vector<int> res;
11+
while(s.size()) {
12+
pair<TreeNode*, int> node = s.top();
13+
s.pop();
14+
if (node.second == White) {
15+
res.push_back(node.first->val);
16+
} else {
17+
if (node.first->right) s.push({node.first->right, Black});
18+
s.push({node.first, White});
19+
if (node.first->left) s.push({node.first->left, Black});
20+
}
21+
}
22+
23+
return res;
24+
25+
}
26+
27+
};

Week02/NOTE.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
学习笔记
1+
学习笔记
2+
3+
树的遍历
4+
1.前中后序遍历
5+
递归写法注意边界条件和顺序。
6+
非递归写法可以用颜色栈比较方便。
7+
2.重建树,已知前中、中后,求剩下的遍历
8+
前序的首即为根,后序的尾即为根,在中序中检索,可以求得左子树、右子树
9+
对于前后序,求中序,则无法确定左右子树,如前{4,7}, 后{7,4}
10+
11+
12+
1.建堆、排序、调整算法
13+
2.大根堆一般用于排序,小根堆一般用于组建优先队列(摘自算法导论)
14+
3.priority_queue<T, container<T>, compare<T>>
15+
默认使用less即大根堆
16+
可以使用lambda表达式作为第三个参数
17+
auto cmp = [](pair<int, int>& a, pair<int, int>& b) {return a.second > b.second;};
18+
priority_queue<pair<int, int> , vector<pair<int, int>>, decltype(cmp)> pq(cmp);
19+
4.当TopK问题中的n取值范围较为固定时,可以使用线性排序算法以取得更优的时间复杂度O(n),如桶排序,计数排序,基数排序。
20+

0 commit comments

Comments
 (0)