Skip to content

Create 31. Next Permutation.md#55

Open
fuga-98 wants to merge 1 commit into
mainfrom
31.-Next-Permutation
Open

Create 31. Next Permutation.md#55
fuga-98 wants to merge 1 commit into
mainfrom
31.-Next-Permutation

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 30, 2025

Comment thread 31. Next Permutation.md
Comment on lines +152 to +156
i = nums_length - 2
while i >= 0:
if nums[i] < nums[i+1]:
break
i -= 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

順序の入れ替え方の関数化などをしてもいいかなと思います。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.4qnnfvpo8ij5

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.

Leetcode上だと短い関数は関数の定義を探す必要があるので、読みにくく感じて避けたのですが、
エディタ上なら分割したほうが読みやすそうです。

Comment thread 31. Next Permutation.md
Comment on lines +160 to +162
j = nums_length - 1
while nums[j] <= nums[i]:
j -= 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これも名前をつけると、後ろから nums[i] 以上であるようなものを見つけるということですね。

Comment thread 31. Next Permutation.md
if nums[i] < nums[i + 1]:
break
i -= 1
j = len(nums) - 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if 文の中で j を使っていないため、 j のスコープを短くするため、 if 文と j の定義を入れ替えたほうがよいと思います。

Comment thread 31. Next Permutation.md
```python
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
def reverse_in_range(lo, hi):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この関数は再利用する可能性がありそうなのでutilのようなモジュールに書くのも選択肢だと思いました。

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.

確かに、これを書いているときにライブラリにないことに驚きました

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.

4 participants