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 6e02078

Browse files
添加题解
1 parent 02db3ae commit 6e02078

7 files changed

+317
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode.cn id=102 lang=cpp
3+
*
4+
* [102] 二叉树的层序遍历
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>> levelOrder(TreeNode* root) {
22+
if (root == nullptr)
23+
return {};
24+
queue<TreeNode*> qe;
25+
qe.push(root);
26+
vector<vector<int>> ans;
27+
while (!qe.empty()) {
28+
int sz = qe.size();
29+
vector<int> level(sz);
30+
for (int i = 0; i < sz; i++) {
31+
TreeNode* n = qe.front();
32+
qe.pop();
33+
level[i] = n->val;
34+
if (n->left != nullptr)
35+
qe.push(n->left);
36+
if (n->right != nullptr)
37+
qe.push(n->right);
38+
}
39+
ans.push_back(level);
40+
}
41+
return ans;
42+
}
43+
};
44+
// @lc code=end
45+
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=107 lang=cpp
3+
*
4+
* [107] 二叉树的层序遍历 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>> levelOrderBottom(TreeNode* root) {
22+
if (root == nullptr)
23+
return {};
24+
queue<TreeNode*> qe;
25+
qe.push(root);
26+
stack<vector<int>> stk;
27+
vector<vector<int>> ans;
28+
while (!qe.empty()) {
29+
int sz = qe.size();
30+
vector<int> level(sz);
31+
for (int i = 0; i < sz; i++) {
32+
TreeNode* n = qe.front();
33+
qe.pop();
34+
level[i] = n->val;
35+
if (n->left != nullptr)
36+
qe.push(n->left);
37+
if (n->right != nullptr)
38+
qe.push(n->right);
39+
}
40+
stk.push(level);
41+
}
42+
while (!stk.empty()) {
43+
ans.push_back(stk.top());
44+
stk.pop();
45+
}
46+
return ans;
47+
}
48+
};
49+
// @lc code=end
50+
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=130 lang=cpp
3+
*
4+
* [130] 被围绕的区域
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
void solve(vector<vector<char>>& board) {
11+
int m = board.size();
12+
int n = board[0].size();
13+
vector<vector<char>> mask(m, vector<char>(n, 'X'));
14+
vector<vector<bool>> visited(m, vector<bool>(n, false));
15+
for (int j = 0; j < n; j++) {
16+
dfs(board, 0, j, mask, visited);
17+
dfs(board, m-1, j, mask, visited);
18+
}
19+
for (int i = 0; i < m; i++) {
20+
dfs(board, i, 0, mask, visited);
21+
dfs(board, i, n-1, mask, visited);
22+
}
23+
for (int i = 0; i < m; i++) {
24+
for (int j = 0; j < n; j++) {
25+
board[i][j] = mask[i][j];
26+
}
27+
}
28+
}
29+
30+
void dfs(vector<vector<char>>& board, int i, int j,
31+
vector<vector<char>>& mask, vector<vector<bool>>& visited) {
32+
if (i < 0 || j < 0 || i >= board.size() || j >= board[0].size())
33+
return;
34+
if (visited[i][j])
35+
return;
36+
visited[i][j] = true;
37+
if (board[i][j] == 'X')
38+
return;
39+
mask[i][j] = 'O';
40+
dfs(board, i-1, j, mask, visited);
41+
dfs(board, i+1, j, mask, visited);
42+
dfs(board, i, j-1, mask, visited);
43+
dfs(board, i, j+1, mask, visited);
44+
}
45+
};
46+
// @lc code=end
47+

‎未整理/BFS/133.克隆图.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @lc app=leetcode.cn id=133 lang=cpp
3+
*
4+
* [133] 克隆图
5+
*/
6+
7+
// @lc code=start
8+
/*
9+
// Definition for a Node.
10+
class Node {
11+
public:
12+
int val;
13+
vector<Node*> neighbors;
14+
Node() {
15+
val = 0;
16+
neighbors = vector<Node*>();
17+
}
18+
Node(int _val) {
19+
val = _val;
20+
neighbors = vector<Node*>();
21+
}
22+
Node(int _val, vector<Node*> _neighbors) {
23+
val = _val;
24+
neighbors = _neighbors;
25+
}
26+
};
27+
*/
28+
29+
class Solution {
30+
public:
31+
Node* cloneGraph(Node* node) {
32+
if (node == nullptr) return nullptr;
33+
unordered_map<Node*, Node*> visited;
34+
queue<Node*> qe;
35+
qe.push(node);
36+
Node* ans = new Node(node->val);
37+
visited[node] = ans;
38+
39+
while (!qe.empty()) {
40+
Node* n = qe.front();
41+
qe.pop();
42+
43+
vector<Node*> v = n->neighbors;
44+
for (auto& item : v) {
45+
if (visited.find(item) == visited.end()) {
46+
// cout << "?? " << v[i]->val << " ";
47+
Node* tmp = new Node(item->val);
48+
visited[item] = tmp;
49+
qe.push(item);
50+
}
51+
visited[n]->neighbors.push_back(visited[item]);
52+
}
53+
}
54+
return ans;;
55+
}
56+
};
57+
// @lc code=end
58+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode.cn id=199 lang=cpp
3+
*
4+
* [199] 二叉树的右视图
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<int> rightSideView(TreeNode* root) {
22+
if (root == nullptr) return {};
23+
queue<TreeNode*> qe;
24+
qe.push(root);
25+
vector<int> ans;
26+
while (!qe.empty()) {
27+
int sz = qe.size();
28+
for (int i = 0; i < sz; i++) {
29+
TreeNode* n = qe.front();
30+
qe.pop();
31+
if (i == 0)
32+
ans.emplace_back(n->val);
33+
if (n->right != nullptr)
34+
qe.push(n->right);
35+
if (n->left != nullptr)
36+
qe.push(n->left);
37+
}
38+
}
39+
return ans;
40+
}
41+
};
42+
// @lc code=end
43+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode.cn id=279 lang=cpp
3+
*
4+
* [279] 完全平方数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int numSquares(int n) {
11+
// 背包问题
12+
vector<int> dp(n + 1, INT_MAX);
13+
dp[0] = 0;
14+
dp[1] = 1;
15+
for (int i = 2; i < n+1; i++) {
16+
for (int j = 1; j <= sqrt(i); j++) {
17+
dp[i] = min(dp[i], dp[i - j*j] + 1);
18+
}
19+
}
20+
return dp[n];
21+
}
22+
};
23+
// @lc code=end
24+
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=884 lang=cpp
3+
*
4+
* [884] 两句话中的不常见单词
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<string> uncommonFromSentences(string s1, string s2) {
11+
vector<string> ans;
12+
unordered_map<string, int> memo;
13+
vector<string> words = split(s1);
14+
for (string& w : words) {
15+
cout << w << " ";
16+
memo[w]++;
17+
}
18+
words = split(s2);
19+
for (string& w : words) {
20+
memo[w]++;
21+
}
22+
for (auto it = memo.begin(); it != memo.end(); it++) {
23+
if (it->second == 1) {
24+
ans.push_back(it->first);
25+
}
26+
}
27+
return ans;
28+
}
29+
30+
vector<string> split(const string& str) {
31+
string s = "";
32+
vector<string> ans;
33+
for (char c : str) {
34+
if (c == ' ') {
35+
if (s != "") {
36+
ans.push_back(s);
37+
}
38+
s = "";
39+
} else {
40+
s += c;
41+
}
42+
}
43+
if (s != "") {
44+
ans.push_back(s);
45+
}
46+
return ans;
47+
}
48+
};
49+
// @lc code=end
50+

0 commit comments

Comments
(0)

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