-
Notifications
You must be signed in to change notification settings - Fork 0
63. Unique paths ii #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| - ^: XOR。numが1なら^numで0b11...10になる | ||
| - このままだと負の数が帰ってしまうので、1bit分のANDを取る | ||
| - gptは^num & 0x01というコードを書いたが、 | ||
| 16進数でANDを取るより2進数で取った方が直感に反しないので変えた |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これはもっと単純に
if obstacleGrid[0][0] == 0 {
return 1
}
return 0
とすればいいでしょう。「obstacleGrid[0][0]が0なら1を、1なら0を返したかった」の文字通りですよね。
さらに、下の条件はobstacleGrid[0][0] == 1 で上に書かれていますね。あと、上は他とまとめられるはずです。
| // gridの始点(0,0)から進められるマスが全て障害である場合、0を返す | ||
| if (rowCount <= 1 || obstacleGrid[1][0] == 1) && (colCount <= 1 || obstacleGrid[0][1] == 1) { | ||
| return 0 | ||
| } | ||
| // gridの終点へ進められるマスが全て障害である場合、0を返す | ||
| if (rowCount <= 1 || obstacleGrid[rowCount-2][colCount-1] == 1) && (colCount <= 1 || obstacleGrid[rowCount-1][colCount-2] == 1) { | ||
| return 0 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここ、下のループとくっつきませんか。(特定の場合に速くなるかもしれませんが。)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こういう感じということでしょうか?
for i := 0; ... {
for i := 0; ... {
if obstacleGrid[i][j] == 1 { ... }
if i == 0 && j == 0 { ... }
if i == 0 && j == 1 {
if obstacleGrid[0][1] == 1 && obstacleGrid[1][0] == 1 {
return 0
}
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、いえ消してしまっていいだろう。
この「pattern defeatingな早期リターン」がないほうがよいという意図です。
| pathCountGrid[0][0] = 1 | ||
| continue | ||
| } | ||
| p := 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この後のコードで変更されてますが、やはりpという名前だと違和感がありますね。
まあ何が入る変数か、読めばすぐわかるのですが、あまりそういうことを読者に求める前提で書かないのが習慣として大事ですね(自戒の念を込めて)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。スタイルガイドも合わせて確認しておきました
https://google.github.io/styleguide/go/decisions#single-letter-variable-names
| - [[0,1,...],[1,...],...] | ||
| - [...,[...,1]] | ||
| - [...,[...,1],[...,1,0]] | ||
| - gridの大きさが1×1の時にobstacleGrid[0][0]が0なら1を、1なら0を返したかったので、 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 - obstacleGrid[0][0]みたいな話でしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、そうです。難しく考えてしまっていました
| pathCounts[c] = 0 | ||
| continue | ||
| } | ||
| if r == 0 && c == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一回しか通らないのでループ外に書いても良いと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここは正しくは「最大1回通る」ですね。(0,0) が obstacle である場合はその前のブロックで処理されて continue します。また、ループの外に出しても (0,1), (1,0) の処理をしないといけないので結局ループを r = range(0, rowCount), c = range(0, columnCount) で回す必要があります。
https://leetcode.com/problems/unique-paths-ii/description/