Conversation
| if (obstacleGrid[0][0] == 1) { | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
obstacleGrid[num_rows - 1][num_columns - 1]の時もpathは0になるので,早期returnできるのではないかと思いました.
There was a problem hiding this comment.
たしかにそうですね!
num_paths[0][0] = 1だとうまくいかないケースを場合分けするついでに早期returnする感覚で書いていたので見落としておりました。
| if (obstacleGrid[i][j] == 1) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
== 1はマジックナンバーになっているので,defineしてあげたらわかりやすいと思います
| if (i != 0) { | ||
| num_paths[i][j] += num_paths[i-1][j]; | ||
| } | ||
| if (j != 0) { | ||
| num_paths[i][j] += num_paths[i][j-1]; | ||
| } |
There was a problem hiding this comment.
処理がシンプルにまとまっていて,とても見やすいと思いました
参考にさせていただきます
| class Solution { | ||
| public: | ||
| int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { | ||
| vector<vector<int>> num_paths(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0)); |
There was a problem hiding this comment.
ご存じかもしれませんが、using namespace std; についてのコメントを共有いたします。
colorbox/leetcode#27 (comment)
There was a problem hiding this comment.
これは把握していなかったので助かります。ありがとうございます。
| return 0; | ||
| } | ||
| num_paths[0][0] = 1; | ||
| for (int i = 0; i < num_rows; i++) { |
There was a problem hiding this comment.
このあたりのコメントを考慮したとき、私の感覚では今回は行列ではなくて座標かなと感じました。
加えて、row, col の方が num_rows, num_cols という命名とも対称性があってよいような気がします。
There was a problem hiding this comment.
確かにnum_rows, num_columnsとの対照性は気になりますね。ありがとうございます。
| return 0; | ||
| } | ||
| num_paths[0][0] = 1; | ||
| for (int i = 0; i < num_rows; i++) { |
There was a problem hiding this comment.
++i のほうがよいかなと思いました。
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement
There was a problem hiding this comment.
この仕様は知らなかったので助かります。ありがとうございます。
問題文:
https://leetcode.com/problems/unique-paths-ii/description/