200. Number of Islands#17
Conversation
| public: | ||
| int m, n; | ||
|
|
||
| void dfs(vector<vector<char>>& grid, int x, int y) { |
There was a problem hiding this comment.
dfs() という関数名は、現場のコードではあまり見かけないように思います。また関数名は動詞の原型・命令形から始めることが多いように思います。 traverse() などはいかがでしょうか?
There was a problem hiding this comment.
そうですね、traverseの方がdfsよりも意味のある関数名だと思います。
| if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '0') { | ||
| return; | ||
| } | ||
| grid[x][y] = '0'; |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
Hiroto-Iizuka/coding_practice#17 (comment)
| int m, n; | ||
|
|
||
| void dfs(vector<vector<char>>& grid, int x, int y) { | ||
| if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '0') { |
There was a problem hiding this comment.
範囲に関する条件式は、一直線上に並べるように書くと読みやすくなると思います。また、 1 つの条件式にあまり項を並べすぎないほうが読みやすくなると思います。
if (!(0 <= x && x < m && 0 <= y && y < n)) {
return;
}
if (grid[x][y] == '0') {
return;
}There was a problem hiding this comment.
そうですね、そちらの方が読みやすくなっていいと思います。
| int m, n; | ||
|
|
||
| void dfs(vector<vector<char>>& grid, int x, int y) { | ||
| if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '0') { |
There was a problem hiding this comment.
x/y の方向が縦/横となってしまっている点に違和感を感じました。 (r, c) (row, col) などを使うと、縦横の方向が一致し、違和感が無くなると思います。
| @@ -0,0 +1,32 @@ | |||
| class Solution { | |||
| public: | |||
| int m, n; | |||
There was a problem hiding this comment.
こちらのコメントをご参照ください。
5ky7/arai60#22 (comment)
また、メンバー変数の名前は、メンバー変数であることを明示するような命名にする場合があります。
https://google.github.io/styleguide/cppguide.html#Variable_Names
Data members of classes (but not structs) additionally have trailing underscores. For instance: a_local_variable, a_struct_data_member, a_class_data_member_.
| n = grid[0].size(); | ||
| int count = 0; | ||
|
|
||
| for(int i = 0; i < m; i++) { |
There was a problem hiding this comment.
for 文のあとにスペースを空けることが多いと思います。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
if (b) { // Space after the keyword in conditions and loops.
この問題:https://leetcode.com/problems/number-of-islands/description/
次に解く問題:https://leetcode.com/problems/max-area-of-island/description/