Conversation
| index -= 1 | ||
| return index | ||
|
|
||
| if len(nums) == 1: |
There was a problem hiding this comment.
index = len(nums) - 2でindex = -1となるので不要でした
| def _find_first_not_sorted_index(nums: List[int])->int: | ||
| # 後ろから走査し, nums[index] >= nums[index+1]ではなくなる初めてのindexを取り出す | ||
| index = len(nums) - 2 | ||
| while index > -1 and nums[index] >= nums[index+1]: |
There was a problem hiding this comment.
index > -1 よりindex >= 0の方が分かりやすい気がします。
| index -= 1 | ||
| return index | ||
|
|
||
| def _find_second_largest_index(nums: List[int], compare_index: int)->int: |
There was a problem hiding this comment.
second largestは2番目に大きい数なので適切な表現ではないと思います。
rfind_bigger_than(num)なんかでどうでしょう。
numsは引数に含める必要はありません。
|
|
||
| nums[right], nums[left] = nums[left], nums[right] | ||
|
|
||
| nums[left+1:] = sorted(nums[left+1:]) |
There was a problem hiding this comment.
降順にソートされているので、reverseすればいいです。コピーするより、in-placeの方の関数を使った方がいいでしょう。
| @@ -0,0 +1,35 @@ | |||
| class Solution: | |||
There was a problem hiding this comment.
pivot_index、swap_indexをコメントで定義して、使うのもありだと思います。
|
|
||
| Reference: | ||
| https://github.com/shining-ai/leetcode/pull/58/files 解くことに夢中になりすぎて関数での分割を忘れていた。 | ||
| nums[pivot_index+1:] = reversed(nums[pivot_index+1:])よりもnums[left + 1 :] = sorted(nums[left + 1 :]) |
There was a problem hiding this comment.
[]内の演算子の周りにスペースを入れるか入れないかはPEPではどうなっていますか?少なくとも一貫性のある書き方にはしてほしいです。
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| def _find_first_not_sorted_index() -> int: |
There was a problem hiding this comment.
_rfind_bigger_thanに合わせるならこちら_rfind_first_not_sorted_indexじゃないですか?
There was a problem hiding this comment.
not_sortedよりnot_decreasingの方がより関数内の処理が具体化されると思います。
|
|
||
| right = _rfind_bigger_than(left) | ||
| nums[left], nums[right] = nums[right], nums[left] | ||
| nums[left+1:].reverse() |
There was a problem hiding this comment.
たぶんこの部分意図した挙動になっていないと思います。
Pythonのsliceは新しいリストを作ります。
https://www.reddit.com/r/Python/comments/rfm6ph/does_array_slicing_use_extra_memory/
https://github.com/python/cpython/blob/a62be77266b1beadd42d4952186332bc0847b7d6/Objects/listobject.c#L465
There was a problem hiding this comment.
知らなかったです。あと多分leetcodeでテストしたつもりになっていましたが多分し忘れていました。今in-place処理にしたphase6を追加してみました。
| right = _rfind_bigger_than(left) | ||
| nums[left], nums[right] = nums[right], nums[left] | ||
|
|
||
| swap_right = len(nums) - 1 |
| nums[swap_right], nums[swap_left] = nums[swap_left], nums[swap_right] | ||
| swap_left += 1 | ||
| swap_right -= 1 | ||
| return No newline at end of file |
| index -= 1 | ||
| return index | ||
|
|
||
| left = _rfind_first_not_decreasing_index() |
There was a problem hiding this comment.
19行目の空行だけ余分な気がします。23と26はあってもなくても良さそうな感じですかね。
|
空行に関して, トップレベルの関数やクラスは、2行ずつ空けて、クラス内部では、1行ずつ空けてメソッドを定義する。とあったのでそれに従ってやってみます |
|
なんだかまだ空行の有無がよくわからない感じですね。 right = _rfind_bigger_than(left) |
|
意味の塊ごとに適当に区切ればいいかと思いますが、例えばphase3.pyとかは多すぎるような気がします。 |
問題
https://leetcode.com/problems/next-permutation/description/?envType=list&envId=xo2bgr0r