Conversation
| yield (i, j + delta) | ||
|
|
||
| def dfs(pos, grid): | ||
| grid[pos[0]][pos[1]] = "0" |
There was a problem hiding this comment.
そうですね。自分で破壊非破壊を意識できるようになりたいです。
| 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) |
There was a problem hiding this comment.
駄目とは言わないんですが、やりたいことに対して少し過剰という印象があります。
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 内包表記とかありますけれども、これくらいのサイズならばリストで十分でしょうかね。
There was a problem hiding this comment.
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]: | |||
There was a problem hiding this comment.
この部分の意図が分かりづらく感じました。
grid または grid[0] が None でないことを確認しているということであれば、 is None を使用することをお勧めいたします。
ksaito0629/leetcode_arai60#1 (comment)
There was a problem hiding this comment.
二次元配列であることを確認する意図でした。
Noneの場合にisを使うのは覚えておきます
| if inside_grid(i, j + delta): | ||
| yield (i, j + delta) | ||
|
|
||
| def dfs(pos, grid): |
There was a problem hiding this comment.
関数名は動詞の命令形から始めることが多いと思います。 traverse() はいかがでしょうか?
There was a problem hiding this comment.
良い名前だと思いました。「関数名は動詞の命令形から始めることが多い」は覚えておきます。
| if not grid or not grid[0]: | ||
| return 0 | ||
|
|
||
| def inside_grid(i, j): |
There was a problem hiding this comment.
(i, j) は、行列の添え字としては時々見かけるのですが、座標を表す変数としては、あまり見かけない印象です。 (r, c) (row, col) などはいかがでしょうか? (x, y) を使う場合もあるのですが、 grid[y][x] と順序が逆順になってしまうため、今回の場合はやや不自然に感じます。
There was a problem hiding this comment.
(i, j) は、行列の添え字ですか、言われてみたらそんな気がしてきました。
(r, c) (row, col) の方が良さそうですね。
https://leetcode.com/problems/number-of-islands/description/