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 0c0b250

Browse files
新增部分题目解答
1 parent d6c1a1a commit 0c0b250

20 files changed

+776
-0
lines changed

‎未整理/DFS/100.相同的树.cpp‎

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=100 lang=cpp
3+
*
4+
* [100] 相同的树
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+
bool isSameTree(TreeNode* p, TreeNode* q) {
22+
if (p == nullptr && q == nullptr) return true;
23+
if (p == nullptr || q == nullptr) return false;
24+
if (p->val != q->val) return false;
25+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
26+
}
27+
};
28+
// @lc code=end
29+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=cpp
3+
*
4+
* [101] 对称二叉树
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+
bool isSymmetric(TreeNode* root) {
22+
return symmetric(root->left, root->right);
23+
}
24+
25+
bool symmetric(TreeNode* p, TreeNode* q) {
26+
if (p == nullptr && q == nullptr) return true;
27+
if (p == nullptr || q == nullptr) return false;
28+
if (p->val != q->val) return false;
29+
return symmetric(p->left, q->right) && symmetric(p->right, q->left);
30+
}
31+
};
32+
// @lc code=end
33+
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=104 lang=cpp
3+
*
4+
* [104] 二叉树的最大深度
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 maxDepth(TreeNode* root) {
22+
if (root == nullptr) return 0;
23+
int a = maxDepth(root->left);
24+
int b = maxDepth(root->right);
25+
return max(a, b) + 1;
26+
}
27+
};
28+
// @lc code=end
29+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode.cn id=108 lang=cpp
3+
*
4+
* [108] 将有序数组转换为二叉搜索树
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+
22+
TreeNode* sortedArrayToBST(vector<int>& nums) {
23+
return genBST(nums, 0, nums.size() - 1);
24+
}
25+
26+
TreeNode* genBST(vector<int>& nums, int begin, int end) {
27+
if (begin > end) return nullptr;
28+
int mid = (end + begin) / 2;
29+
TreeNode* root = new TreeNode(nums[mid]);
30+
root->left = genBST(nums, begin, mid-1);
31+
root->right = genBST(nums, mid+1, end);
32+
return root;
33+
}
34+
};
35+
36+
37+
38+
39+
40+
41+
42+
43+
// @lc code=end
44+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode.cn id=110 lang=cpp
3+
*
4+
* [110] 平衡二叉树
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+
bool isBalanced(TreeNode* root) {
22+
if (root == nullptr) return true;
23+
return dfs(root) != -1;
24+
}
25+
26+
int dfs(TreeNode* root) {
27+
if (root == nullptr)
28+
return 0;
29+
int a = dfs(root->left);
30+
int b = dfs(root->right);
31+
if (a == -1 || b == -1) return -1;
32+
if (abs(a-b) > 1) return -1;
33+
return max(a, b) + 1;
34+
}
35+
};
36+
// @lc code=end
37+

‎未整理/DFS/112.路径总和.cpp‎

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=112 lang=cpp
3+
*
4+
* [112] 路径总和
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+
bool hasPathSum(TreeNode* root, int targetSum) {
22+
int sum = 0;
23+
return traceback(root, targetSum, sum);
24+
}
25+
26+
bool traceback(TreeNode* root, int targetSum, int sum) {
27+
if (root == nullptr) return false;
28+
if (root->left == nullptr && root->right == nullptr)
29+
return (sum + root->val) == targetSum;
30+
return traceback(root->left, targetSum, sum + root->val) ||
31+
traceback(root->right, targetSum, sum + root->val);
32+
33+
}
34+
};
35+
// @lc code=end
36+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode.cn id=257 lang=cpp
3+
*
4+
* [257] 二叉树的所有路径
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<string> binaryTreePaths(TreeNode* root) {
22+
vector<string> ans;
23+
vector<TreeNode*> path;
24+
dfs(root, path, ans);
25+
return ans;
26+
}
27+
28+
void dfs(TreeNode* root, vector<TreeNode*>& path, vector<string>& ans) {
29+
if (root == nullptr) return;
30+
if (root->left == nullptr && root->right == nullptr) {
31+
string s;
32+
for (auto node : path) {
33+
s =s + to_string(node->val) + "->";
34+
}
35+
s += to_string(root->val);
36+
ans.push_back(s);
37+
} else {
38+
path.push_back(root);
39+
dfs(root->left, path, ans);
40+
dfs(root->right, path, ans);
41+
path.pop_back();
42+
}
43+
}
44+
45+
};
46+
// @lc code=end
47+

‎未整理/DFS/733.图像渲染.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode.cn id=733 lang=cpp
3+
*
4+
* [733] 图像渲染
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
11+
int pixel = image[sr][sc];
12+
if (pixel == color)
13+
return image;
14+
dfs(image, sr, sc, pixel, color);
15+
return image;
16+
}
17+
18+
void dfs(vector<vector<int>>& image, int i, int j, int pixel, int color) {
19+
// cout << i << " " << j << endl;
20+
if (i < 0 || j < 0)
21+
return;
22+
if (i >= image.size() || j >= image[0].size())
23+
return;
24+
if (image[i][j] == pixel) {
25+
image[i][j] = color;
26+
dfs(image, i-1, j, pixel, color);
27+
dfs(image, i+1, j, pixel, color);
28+
dfs(image, i, j-1, pixel, color);
29+
dfs(image, i, j+1, pixel, color);
30+
}
31+
}
32+
};
33+
// @lc code=end
34+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @lc app=leetcode.cn id=331 lang=cpp
3+
*
4+
* [331] 验证二叉树的前序序列化
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool isValidSerialization(string preorder) {
11+
12+
}
13+
};
14+
// @lc code=end
15+

‎未整理/栈/1078.bigram-分词.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @lc app=leetcode.cn id=1078 lang=cpp
3+
*
4+
* [1078] Bigram 分词
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<string> findOcurrences(string text, string first, string second) {
11+
vector<string> words = split(text, ' ');
12+
vector<string> ans;
13+
14+
for (int i = 0; i < words.size() - 2; i++) {
15+
if (words[i] == first && words[i+1] == second) {
16+
ans.push_back(words[i+2]);
17+
}
18+
}
19+
return ans;
20+
}
21+
22+
vector<string> split(const string& text, char sp) {
23+
vector<string> ans;
24+
string a = "";
25+
for (char c : text) {
26+
if (c == sp) {
27+
if (a != "")
28+
ans.push_back(a);
29+
a = "";
30+
continue;
31+
} else {
32+
a = a + c;
33+
}
34+
}
35+
if (a != "")
36+
ans.push_back(a);
37+
return ans;
38+
}
39+
};
40+
// @lc code=end
41+

0 commit comments

Comments
(0)

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