Skip to content

Graph bfs dfs 200#17

Open
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
grapph-bfs-dns-200
Open

Graph bfs dfs 200#17
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
grapph-bfs-dns-200

Conversation

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner

@hiroki-horiguchi-dev hiroki-horiguchi-dev commented May 20, 2026

200. Number of Islandsを解きました。
レビューお願いします。

@hiroki-horiguchi-dev hiroki-horiguchi-dev changed the title Update 200.md 200.md May 20, 2026
Copy link
Copy Markdown

@jjysogfy jjysogfy left a comment

Choose a reason for hiding this comment

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

いくつかコメントさせてもらいました。何かあればお気軽にコメントいただければと思います。

Comment thread graph-bfs-dns/200.md
- 問題: [200. Number of Islands](https://leetcode.com/problems/number-of-islands/submissions/2008160365/)
- コメント集: [200. Number of Islands](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy)
- 一応全て確認したが、Python関係の話が多かったと思う
- コメント先のPullRequestで、column, row の変数名の話と引数を変更するのはどうなんだ?という話があったので考えてみた
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

考えてみた内容か、それを踏まえたコードがあると良いと思います。
変数名を変えた方が良いと思ったのかそうでないのか(あるいはよくわからないか)、読み取れずレビューで迷いました。

Comment thread graph-bfs-dns/200.md

while (!lands.isEmpty()) {
int[] index = lands.poll();
int numRow = index[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.

考えているかもしれませんが、この変数名は見づらく感じました。numと付くと「rowの個数」のように見えるからです。
これをrowIndexとし、引数の方はstartRowIndexとするのはどうでしょうか。

Comment thread graph-bfs-dns/200.md
}

private int numIslandsHelper(char[][] grid, int rowIndex, int columnIndex) {
Queue<int[]> lands = new ArrayDeque<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

int[]ではなくrecordを使う選択肢もあると思います。

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[]で十分だと思ってます

Comment thread graph-bfs-dns/200.md
return result;
}

private int numIslandsHelper(char[][] grid, int rowIndex, int columnIndex) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このメソッドは、numIslandsHelperと名付けるにはnumIslandsと違いすぎると思いました。返り値も0か1だけですし。
良い名前が思いつかないですが、たとえばtraverseはどうでしょうか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

同感です。今回は特に副作用としてgridの書き換えも行なっているのでそれがわかるようにしたいですね。
自分が見ていていいなと思ったのはsinkIslandsなどですね。1を0に置き換えることをsinkと表現するのがわかりやすいです

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.

お二人ともありがとうございます、
Traversal は処理の意味として理解できていいですね。
自分的にはsink がかなりしっくり来ました。
陸を沈めるってことですね、いいですね!

Comment thread graph-bfs-dns/200.md
}

while (!lands.isEmpty()) {
int[] index = lands.poll();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいかもしれませんが、キューへの追加と削除についてadd/pollを使っていますが、add/removeかoffer/pollのどちらかだけを使う方が統一感があり好みです。
https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/util/Queue.html
ArrayDequeなのでどちらでも差はなさそうですが。

Copy link
Copy Markdown
Owner Author

@hiroki-horiguchi-dev hiroki-horiguchi-dev May 23, 2026

Choose a reason for hiding this comment

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

コメントありがとうございます、読みました。
統一感、なるほど確かにそうかもしれないですね。実際私のコードでは統一されていません。

私の認識で恐縮ですが、どちらかというと、例外投げて欲しければ add/remove, 例外ではなく値が欲しいなら offer/poll を使おうということで、文脈に応じて使い分けるものかなと思っているのですが、どうでしょう?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

すみません、そうですね。間違って覚えていました……。
addとofferはArrayDequeなら同じだと思いますが(ソースコード https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ArrayDeque.java )、removeとpollは空の場合に違いますね。使い分けるものなのだろうと思いました。

Comment thread graph-bfs-dns/200.md
int numColumn = index[1];

// 上下左右を探索して、1ならqueueにつめる
int nextNumRow = numRow + 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは変数に置かないほうが直接的で見やすいと感じました。

Comment thread graph-bfs-dns/200.md
- コメント集: [](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy)
- 問題: [200. Number of Islands](https://leetcode.com/problems/number-of-islands/submissions/2008160365/)
- コメント集: [200. Number of Islands](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy)
- 一応全て確認したが、Python関係の話が多かったと思う
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コメント集にあったか覚えてないですが、今回は与えられた配列を破壊するべきかしないべきかの検討があってもいいと思いました。

例えば、何か島を作る関数が別であった時などにデバッグで使うを想定すると、今回は引数で与えられた配列は破壊したくないですね

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.

ありがとうございます。
ここ、対応しようと思って忘れてました。
取り込みます。

Comment thread graph-bfs-dns/200.md
int result = 0;
for (int rowIndex = 0; rowIndex < grid.length; rowIndex++) {
for (int columnIndex = 0; columnIndex < grid[0].length; columnIndex++) {
result += numIslandsHelper(grid, rowIndex, columnIndex);
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か0が返ってくるだけなので。

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.

おっしゃる通りですね。
別に += する必要がないですね。。

@hiroki-horiguchi-dev hiroki-horiguchi-dev changed the title 200.md Graph bfs dfs 200 May 24, 2026
Comment thread graph-bfs-dns/200.md
```java
class Solution {
public int numIslands(char[][] grid) {
if (grid.length == 0) return 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.

こちらのコメントをご参照ください。
Shunii85/arai60#17 (comment)

Comment thread graph-bfs-dns/200.md

private int numIslandsHelper(char[][] grid, int rowIndex, int columnIndex) {
// index ouf of range
if ((rowIndex < 0 || columnIndex < 0) || (rowIndex >= grid.length || columnIndex >= grid[0].length)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

数直線上に一直線上に並ぶように書いたほうが、読み手にとって読みやすくなると思います。

if (!(0 <= rowIndex && rowIndex < grid.length && 0 <= columnIndex && columnIndex < grid[0].length)) {

Comment thread graph-bfs-dns/200.md
}
// find land
if (grid[rowIndex][columnIndex] == '1') {
grid[rowIndex][columnIndex] = '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)

Comment thread graph-bfs-dns/200.md
private int numIslandsHelper(char[][] grid, int rowIndex, int columnIndex) {
Queue<int[]> lands = new ArrayDeque<>();
// find land
if (grid[rowIndex][columnIndex] == '1') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

もう少しシンプルに early return したほうが、読み手にとって読みやすくなると思います。

if (grid[rowIndex][columnIndex] == '0') {
    return 0;
}

grid[rowIndex][columnIndex] = '0';
lands.add(new int[]{rowIndex, columnIndex});

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.

5 participants