FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

week3: Add leetcode 104, 429, 703 and 997 · feixiangcode/algorithm@d61d886 · GitHub

Commit d61d886

Browse files
authored andcommitted
week3: Add leetcode 104, 429, 703 and 997
1 parent 83e2fd4 commit d61d886

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int maxDepth(TreeNode* root) {
13+
if (root == NULL) {
14+
return 0;
15+
}
16+
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
17+
}
18+
};
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, vector<Node*> _children) {
11+
val = _val;
12+
children = _children;
13+
}
14+
};
15+
*/
16+
class Solution {
17+
public:
18+
vector<vector<int>> levelOrder(Node* root) {
19+
/* 首先判断root是否为空,为空直接返回空数组 */
20+
if (root == NULL) {
21+
return {};
22+
}
23+
24+
vector<vector<int>> res;
25+
queue<Node*> q;
26+
q.push(root);
27+
28+
while (!q.empty()) {
29+
/* 用level定义每层的数组 */
30+
vector<int> level;
31+
for (int i = q.size(); i > 0; --i) {
32+
/* 对t的类型进行自动推导 */
33+
auto t = q.front();
34+
q.pop();
35+
level.push_back(t->val);
36+
/* 子节点放入children数组 */
37+
if (!t->children.empty()) {
38+
/* 对t->children进行遍历 */
39+
for (auto a : t->children) {
40+
q.push(a);
41+
}
42+
}
43+
}
44+
res.push_back(level);
45+
}
46+
return res;
47+
}
48+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class KthLargest {
2+
public:
3+
int n;
4+
priority_queue<int, vector<int>, greater<int>> q; /* 建立最小堆*/
5+
6+
KthLargest(int k, vector<int>& nums) {
7+
n = k;
8+
for (auto i:nums){
9+
add(i);
10+
}
11+
}
12+
13+
int add(int val) {
14+
/* 存放最大的K个数 */
15+
if (q.size() < n) {
16+
q.push(val);
17+
} else {
18+
if (q.top() < val) {
19+
q.push(val);
20+
q.pop();
21+
}
22+
}
23+
24+
return q.top();
25+
}
26+
};
27+
28+
/**
29+
* Your KthLargest object will be instantiated and called as such:
30+
* KthLargest* obj = new KthLargest(k, nums);
31+
* int param_1 = obj->add(val);
32+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findJudge(int N, vector<vector<int>>& trust) {
4+
vector<int> degrees(N+1);
5+
for (const auto & t:trust) {
6+
--degrees[t[0]];
7+
++degrees[t[1]];
8+
}
9+
10+
for (int i = 1; i <= N; i++) {
11+
if (degrees[i] == N - 1) {
12+
return i;
13+
}
14+
}
15+
16+
return -1;;
17+
}
18+
};

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL