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 d55ab2f

Browse files
ChunelFengjunfeng.fjyanglbme
authored
Chunel feng/lcof cpp (#335)
* 更新lcof-cpp版本[机器人的运动范围]题解思路 * 更新lcof-cpp版本[从上到下打印二叉树]源码 * Update README.md * Update Solution.cpp Co-authored-by: junfeng.fj <junfeng.fj@alibaba-inc.com> Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 6ca5b5d commit d55ab2f

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

‎lcof/面试题13. 机器人的运动范围/README.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,54 @@ func dfs(x, y, m, n, k int, visited [][]bool) int {
173173
}
174174
```
175175

176+
### **C++**
177+
178+
```cpp
179+
class Solution {
180+
public:
181+
int checksum(int m, int n, int target) {
182+
int a = 0;
183+
while (m > 0) {
184+
a += m % 10;
185+
m /= 10;
186+
}
187+
188+
int b = 0;
189+
while (n > 0) {
190+
b += n % 10;
191+
n /= 10;
192+
}
193+
194+
return a + b <= target;
195+
}
196+
197+
int moving(int row, int col, vector<vector<int>>& arr, int i, int j, int target) {
198+
int count = 0;
199+
if (checksum(i, j, target)
200+
&& i>=0 && i < row && j>=0 && j < col
201+
&& arr[i][j] == 0) {
202+
arr[i][j] = 1;
203+
count = 1 + moving(row, col, arr, i-1, j, target)
204+
+ moving(row, col, arr, i, j-1, target)
205+
+ moving(row, col, arr, i+1, j, target)
206+
+ moving(row, col, arr, i, j+1, target);
207+
}
208+
209+
return count;
210+
}
211+
212+
int movingCount(int m, int n, int k) {
213+
if (m == 0 || n == 0) {
214+
return 0;
215+
}
216+
217+
vector<vector<int>> arr(m, vector<int>(n, 0));
218+
int cnt = moving(m, n, arr, 0, 0, k);
219+
return cnt;
220+
}
221+
};
222+
```
223+
176224
### **...**
177225
178226
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int checksum(int m, int n, int target) {
4+
int a = 0;
5+
while (m > 0) {
6+
a += m % 10;
7+
m /= 10;
8+
}
9+
int b = 0;
10+
while (n > 0) {
11+
b += n % 10;
12+
n /= 10;
13+
}
14+
return a + b <= target;
15+
}
16+
int moving(int row, int col, vector<vector<int>>& arr, int i, int j, int target) {
17+
int count = 0;
18+
if (checksum(i, j, target)
19+
&& i>=0 && i < row && j>=0 && j < col
20+
&& arr[i][j] == 0) {
21+
arr[i][j] = 1;
22+
count = 1 + moving(row, col, arr, i-1, j, target)
23+
+ moving(row, col, arr, i, j-1, target)
24+
+ moving(row, col, arr, i+1, j, target)
25+
+ moving(row, col, arr, i, j+1, target);
26+
}
27+
return count;
28+
}
29+
int movingCount(int m, int n, int k) {
30+
if (m == 0 || n == 0) {
31+
return 0;
32+
}
33+
vector<vector<int>> arr(m, vector<int>(n, 0));
34+
int cnt = moving(m, n, arr, 0, 0, k);
35+
return cnt;
36+
}
37+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
11+
class Solution {
12+
public:
13+
vector<int> levelOrder(TreeNode* root) {
14+
vector<int> ret;
15+
if (!root) {
16+
return ret;
17+
}
18+
19+
queue<TreeNode *> q;
20+
q.push(root);
21+
22+
while (!q.empty()) {
23+
auto head = q.front();
24+
q.pop();
25+
ret.push_back(head->val);
26+
if (head->left) {
27+
q.push(head->left);
28+
}
29+
30+
if (head->right) {
31+
q.push(head->right);
32+
}
33+
}
34+
35+
return ret;
36+
}
37+
};

0 commit comments

Comments
(0)

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