Skip to content

31. Next Permutation#35

Open
n6o wants to merge 1 commit into
mainfrom
next-permutation
Open

31. Next Permutation#35
n6o wants to merge 1 commit into
mainfrom
next-permutation

Conversation

@n6o
Copy link
Copy Markdown
Owner

@n6o n6o commented May 10, 2026

今回の問題

Next Permutation - LeetCode

使用言語

Python

for i in reversed(range(pivot + 1, len(nums))):
if nums[i] > nums[pivot]:
nums[i], nums[pivot] = nums[pivot], nums[i]
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

返り値のない関数で return をつけるかどうかは統一した方が良いと思いました

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これはearly returnではないですかね?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

その通りですね。この指摘は無視してください、すみません。

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.

ありがとうございます。
あまり意識はしていなかったので、lint 設定などを入れてみます。

https://docs.astral.sh/ruff/rules/#flake8-return-ret

Do not return anything, modify nums in-place instead.
"""

def search_pivot(nums: List[int]) -> int | None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nextPermutationの内部で定義するなら、nums を引数で与えなくても良いのではないかと思いました

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.

ありがとうございます。
ここはちょっと考えたところでした。

関数にする際に nums を更新する reverseswap_pivot_with_successor があり、引数として渡した方が読む時にわかりやすいかなと考えました。
search_pivot は読み取りのみなので、その基準からはクロージャーでもいいのですが、形式を合わせるために引数に入れました。

ここも一貫性を持って実装するようにします。

nums[i], nums[pivot] = nums[pivot], nums[i]
return

pivot = search_pivot(nums)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

逆転が見つからない時のpivotを-1にすると場合分けが不要になると思いました

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.

ありがとうございます。ここは確認していませんでした。
早期リターンする場合、それを取り除いても動くか、余計なコストがないかなども考えるようにします。

length = len(nums)
pivot = -1

for i in reversed(range(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.

range の戻り値を reverse で反転させるのは、あまり見ないように思います。

for i in range(len(nums) - 1, -1, -1)):

と言った書き方をよく見かけるように思います。

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.

ありがとうございます。
Python の普通の書き方なのか判断できなかったので助かりますmm
上記の記法で書くようにします。

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