Skip to content

200. Number of Islands#17

Open
hemispherium wants to merge 1 commit into
mainfrom
200-number-of-islands
Open

200. Number of Islands#17
hemispherium wants to merge 1 commit into
mainfrom
200-number-of-islands

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 9, 2026
public:
int m, n;

void dfs(vector<vector<char>>& grid, int x, int y) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dfs() という関数名は、現場のコードではあまり見かけないように思います。また関数名は動詞の原型・命令形から始めることが多いように思います。 traverse() などはいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね、traverseの方がdfsよりも意味のある関数名だと思います。

if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == '0') {
return;
}
grid[x][y] = '0';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
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') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

範囲に関する条件式は、一直線上に並べるように書くと読みやすくなると思います。また、 1 つの条件式にあまり項を並べすぎないほうが読みやすくなると思います。

if (!(0 <= x && x < m && 0 <= y && y < n)) {
    return;
}

if (grid[x][y] == '0') {
    return;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね、そちらの方が読みやすくなっていいと思います。

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') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x/y の方向が縦/横となってしまっている点に違和感を感じました。 (r, c) (row, col) などを使うと、縦横の方向が一致し、違和感が無くなると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

承知しました!

@@ -0,0 +1,32 @@
class Solution {
public:
int m, n;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
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++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for 文のあとにスペースを空けることが多いと思います。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

if (b) { // Space after the keyword in conditions and loops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants