Skip to content

200. Number of Islands#16

Open
tom4649 wants to merge 1 commit intomainfrom
200.Number-of-Islands
Open

200. Number of Islands#16
tom4649 wants to merge 1 commit intomainfrom
200.Number-of-Islands

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 15, 2026

yield (i, j + delta)

def dfs(pos, grid):
grid[pos[0]][pos[1]] = "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.

grid の破壊的変更は、numIslands 呼んだ人がびっくりしませんかね。

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 on lines +9 to +16
def get_adjacent(pos):
i = pos[0]
j = pos[1]
for delta in [1, -1]:
if inside_grid(i + delta, j):
yield (i + delta, j)
if inside_grid(i, j + delta):
yield (i, j + delta)
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 ni, nj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
        if inside_grid(ni, nj):
            yield ni, nj

くらいでもよくないですかね。
Generator 内包表記とかありますけれども、これくらいのサイズならばリストで十分でしょうかね。

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.

4つの繰り返しなのでたしかに 大袈裟でしたね。
Generator 内包表記、知らなかったです
https://docs.python.org/3/reference/expressions.html#generator-expressions

@@ -0,0 +1,31 @@
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
if not grid or not grid[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.

この部分の意図が分かりづらく感じました。
grid または grid[0] が None でないことを確認しているということであれば、 is None を使用することをお勧めいたします。
ksaito0629/leetcode_arai60#1 (comment)

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.

二次元配列であることを確認する意図でした。
Noneの場合にisを使うのは覚えておきます

if inside_grid(i, j + delta):
yield (i, j + delta)

def dfs(pos, grid):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

関数名は動詞の命令形から始めることが多いと思います。 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.

良い名前だと思いました。「関数名は動詞の命令形から始めることが多い」は覚えておきます。

if not grid or not grid[0]:
return 0

def inside_grid(i, j):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(i, j) は、行列の添え字としては時々見かけるのですが、座標を表す変数としては、あまり見かけない印象です。 (r, c) (row, col) などはいかがでしょうか? (x, y) を使う場合もあるのですが、 grid[y][x] と順序が逆順になってしまうため、今回の場合はやや不自然に感じます。

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.

(i, j) は、行列の添え字ですか、言われてみたらそんな気がしてきました。
(r, c) (row, col) の方が良さそうですね。

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.

3 participants