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

Chunel feng/lcof cpp #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 4 commits into doocs:main from ChunelFeng:ChunelFeng/lcof-cpp
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lcof/面试题13. 机器人的运动范围/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,54 @@ func dfs(x, y, m, n, k int, visited [][]bool) int {
}
```

### **C++**

```cpp
class Solution {
public:
int checksum(int m, int n, int target) {
int a = 0;
while (m > 0) {
a += m % 10;
m /= 10;
}

int b = 0;
while (n > 0) {
b += n % 10;
n /= 10;
}

return a + b <= target;
}

int moving(int row, int col, vector<vector<int>>& arr, int i, int j, int target) {
int count = 0;
if (checksum(i, j, target)
&& i>=0 && i < row && j>=0 && j < col
&& arr[i][j] == 0) {
arr[i][j] = 1;
count = 1 + moving(row, col, arr, i-1, j, target)
+ moving(row, col, arr, i, j-1, target)
+ moving(row, col, arr, i+1, j, target)
+ moving(row, col, arr, i, j+1, target);
}

return count;
}

int movingCount(int m, int n, int k) {
if (m == 0 || n == 0) {
return 0;
}

vector<vector<int>> arr(m, vector<int>(n, 0));
int cnt = moving(m, n, arr, 0, 0, k);
return cnt;
}
};
```

### **...**

```
Expand Down
37 changes: 37 additions & 0 deletions lcof/面试题13. 机器人的运动范围/Solution.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
int checksum(int m, int n, int target) {
int a = 0;
while (m > 0) {
a += m % 10;
m /= 10;
}
int b = 0;
while (n > 0) {
b += n % 10;
n /= 10;
}
return a + b <= target;
}
int moving(int row, int col, vector<vector<int>>& arr, int i, int j, int target) {
int count = 0;
if (checksum(i, j, target)
&& i>=0 && i < row && j>=0 && j < col
&& arr[i][j] == 0) {
arr[i][j] = 1;
count = 1 + moving(row, col, arr, i-1, j, target)
+ moving(row, col, arr, i, j-1, target)
+ moving(row, col, arr, i+1, j, target)
+ moving(row, col, arr, i, j+1, target);
}
return count;
}
int movingCount(int m, int n, int k) {
if (m == 0 || n == 0) {
return 0;
}
vector<vector<int>> arr(m, vector<int>(n, 0));
int cnt = moving(m, n, arr, 0, 0, k);
return cnt;
}
};
37 changes: 37 additions & 0 deletions lcof/面试题32 - I. 从上到下打印二叉树/Solution.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

class Solution {
public:
vector<int> levelOrder(TreeNode* root) {
vector<int> ret;
if (!root) {
return ret;
}

queue<TreeNode *> q;
q.push(root);

while (!q.empty()) {
auto head = q.front();
q.pop();
ret.push_back(head->val);
if (head->left) {
q.push(head->left);
}

if (head->right) {
q.push(head->right);
}
}

return ret;
}
};

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