三角形最大周长 + 最大宽度坡 + 有效的数独 + 岛屿数量

2019-6-20刷题

三角形最大周长

https://leetcode-cn.com/articles/largest-perimeter-triangle/

最大宽度坡

https://leetcode-cn.com/articles/maximum-width-ramp/

有效的数独

https://leetcode-cn.com/articles/valid-sudoku/

岛屿数量

https://leetcode-cn.com/articles/largest-perimeter-triangle/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void (vector<vector<char>>& grid, int x, int y) {
if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == '1') {
grid[x][y] = '0';
bfs(grid, x - 1, y);
bfs(grid, x + 1, y);
bfs(grid, x, y - 1);
bfs(grid, x, y + 1);
}
}

int numIslands(vector<vector<char>>& grid) {
int sum = 0;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] == '1') {
sum++;
bfs(grid, i, j);
}
}
}
return sum;
}
#简洁且优雅!

作者:taojing96
链接:https://leetcode-cn.com/problems/two-sum/solution/kan-liao-niu-ke-de-zuo-shen-shi-pin-xue-dao-de-ye-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

知识点

1
2
3
4
collection.Counter如何使用?
python中字典的键不存在会不会报错?
python中如何按照第几个元素来排序?
python/C++中如何使用bfs dfs?