Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d21d5a1

Browse files
增加部分题目
1 parent 6e02078 commit d21d5a1

11 files changed

+406
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @lc app=leetcode.cn id=113 lang=cpp
3+
*
4+
* [113] 路径总和 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
22+
vector<int> path;
23+
vector<vector<int>> ans;
24+
traceback(root, targetSum, 0, path, ans);
25+
return ans;
26+
}
27+
28+
void traceback(TreeNode* root, int targetSum, int sum, vector<int>& path, vector<vector<int>>& ans) {
29+
if (root == nullptr) return;
30+
sum += root->val;
31+
path.push_back(root->val);
32+
if (root->left == nullptr && root->right == nullptr && sum == targetSum)
33+
{
34+
ans.push_back(path);
35+
}
36+
traceback(root->left, targetSum, sum, path, ans);
37+
traceback(root->right, targetSum, sum, path, ans);
38+
path.pop_back();
39+
}
40+
};
41+
// @lc code=end
42+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode.cn id=117 lang=cpp
3+
*
4+
* [117] 填充每个节点的下一个右侧节点指针 II
5+
*/
6+
7+
// @lc code=start
8+
/*
9+
// Definition for a Node.
10+
class Node {
11+
public:
12+
int val;
13+
Node* left;
14+
Node* right;
15+
Node* next;
16+
17+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
18+
19+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
20+
21+
Node(int _val, Node* _left, Node* _right, Node* _next)
22+
: val(_val), left(_left), right(_right), next(_next) {}
23+
};
24+
*/
25+
26+
class Solution {
27+
public:
28+
Node* connect(Node* root) {
29+
if (root == nullptr) return nullptr;
30+
queue<Node*> qe;
31+
qe.push(root);
32+
while (!qe.empty()) {
33+
int sz = qe.size();
34+
Node* next = nullptr;
35+
for (int i = 0; i < sz; i++) {
36+
Node* n = qe.front();
37+
qe.pop();
38+
n->next = next;
39+
next = n;
40+
if (n->right != nullptr)
41+
qe.push(n->right);
42+
if (n->left != nullptr)
43+
qe.push(n->left);
44+
}
45+
}
46+
return root;
47+
}
48+
};
49+
// @lc code=end
50+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=129 lang=cpp
3+
*
4+
* [129] 求根节点到叶节点数字之和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
int sumNumbers(TreeNode* root) {
22+
if (root == nullptr) return 0;
23+
int sum = 0;
24+
traceback(root, sum, 0);
25+
return sum;
26+
}
27+
28+
void traceback(TreeNode* root, int& sum, int num) {
29+
if (root == nullptr) return;
30+
num = num * 10 + root->val;
31+
if (root->left == nullptr && root->right == nullptr)
32+
{
33+
sum += num;
34+
}
35+
traceback(root->left, sum, num);
36+
traceback(root->right, sum, num);
37+
}
38+
};
39+
// @lc code=end
40+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=933 lang=cpp
3+
*
4+
* [933] 最近的请求次数
5+
*/
6+
7+
// @lc code=start
8+
class RecentCounter {
9+
public:
10+
RecentCounter() {
11+
}
12+
13+
int ping(int t) {
14+
q.push(t);
15+
while(q.front() < t-3000)
16+
q.pop();
17+
return q.size();
18+
}
19+
20+
private:
21+
queue<int> q;
22+
};
23+
24+
/**
25+
* Your RecentCounter object will be instantiated and called as such:
26+
* RecentCounter* obj = new RecentCounter();
27+
* int param_1 = obj->ping(t);
28+
*/
29+
// @lc code=end
30+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=1022 lang=cpp
3+
*
4+
* [1022] 从根到叶的二进制数之和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
int sumRootToLeaf(TreeNode* root) {
22+
int sum = 0;
23+
dfs(root, 0, sum);
24+
return sum;
25+
}
26+
27+
void dfs(TreeNode* root, int path, int& sum) {
28+
if (root == nullptr)
29+
return;
30+
int val = (path << 1) | root->val;
31+
if (root->left == nullptr && root->right == nullptr) {
32+
sum += val;
33+
}
34+
dfs(root->left, val, sum);
35+
dfs(root->right, val, sum);
36+
}
37+
38+
};
39+
// @lc code=end
40+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=1030 lang=cpp
3+
*
4+
* [1030] 距离顺序排列矩阵单元格
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
11+
vector<vector<vector<int>>> cup(200);
12+
for (int i = 0; i < rows; i++) {
13+
for (int j = 0; j < cols; j++) {
14+
int a = abs(i - rCenter);
15+
int b = abs(j - cCenter);
16+
int dis = a + b;
17+
cup[dis].push_back({ i, j });
18+
}
19+
}
20+
vector<vector<int>> ans;
21+
for (int i = 0; i < cup.size(); i++) {
22+
for (int j = 0; j < cup[i].size(); j++) {
23+
ans.push_back(cup[i][j]);
24+
}
25+
}
26+
return ans;
27+
28+
}
29+
};
30+
// @lc code=end
31+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=129 lang=cpp
3+
*
4+
* [129] 求根节点到叶节点数字之和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
int sumNumbers(TreeNode* root) {
22+
if (root == nullptr) return 0;
23+
int sum = 0;
24+
traceback(root, sum, 0);
25+
return sum;
26+
}
27+
28+
void traceback(TreeNode* root, int& sum, int num) {
29+
if (root == nullptr) return;
30+
num = num * 10 + root->val;
31+
if (root->left == nullptr && root->right == nullptr)
32+
{
33+
sum += num;
34+
}
35+
traceback(root->left, sum, num);
36+
traceback(root->right, sum, num);
37+
}
38+
};
39+
// @lc code=end
40+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode.cn id=744 lang=cpp
3+
*
4+
* [744] 寻找比目标字母大的最小字母
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
char nextGreatestLetter(vector<char>& letters, char target) {
11+
// if (target > letters[letters.size() - 1])
12+
// return letters[0];
13+
// for (char c : letters) {
14+
// if (c > target)
15+
// return c;
16+
// }
17+
// return letters[0];
18+
vector<char>::iterator c = upper_bound(letters.begin(), letters.end(), target);
19+
if (c == letters.end())
20+
return letters[0];
21+
return *c;
22+
}
23+
};
24+
// @lc code=end
25+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=766 lang=cpp
3+
*
4+
* [766] 托普利茨矩阵
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
11+
int n = matrix.size();
12+
int m = matrix[0].size();
13+
vector<vector<bool>> visited(n, vector<bool>(m, false));
14+
return dfs(matrix, 0, matrix[0].size() - 1, visited);
15+
}
16+
17+
bool dfs(const vector<vector<int>>& matrix, int i, int j, vector<vector<bool>>& visited) {
18+
if (j <= 0 || i >= matrix.size() -1)
19+
return true;
20+
if (visited[i][j])
21+
return true;
22+
if (matrix[i][j-1] != matrix[i+1][j])
23+
return false;
24+
visited[i][j] = true;
25+
return dfs(matrix, i, j-1, visited) && dfs(matrix, i+1, j, visited);
26+
}
27+
};
28+
// @lc code=end
29+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode.cn id=922 lang=cpp
3+
*
4+
* [922] 按奇偶排序数组 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<int> sortArrayByParityII(vector<int>& nums) {
11+
int i = 0;
12+
int j = 1;
13+
for (int n : nums) {
14+
int o = n & 0xFFFF;
15+
// cout << n << " " << o << " " << i << " " << j << " ";
16+
if (o % 2 == 0) {
17+
o = o << 16;
18+
// cout << o << endl;
19+
nums[i] = nums[i] | o;
20+
i += 2;
21+
} else {
22+
o = o << 16;
23+
// cout << o << endl;
24+
nums[j] = nums[j] | o;
25+
j += 2;
26+
}
27+
}
28+
for (int i = 0; i < nums.size(); i++) {
29+
// cout << i << "." << nums[i] << " " << (nums[i] >> 16) << endl;
30+
nums[i] = nums[i] >> 16;
31+
}
32+
return nums;
33+
}
34+
};
35+
// @lc code=end
36+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /