Skip to content

Create 31. Next Permutation.md#58

Open
tokuhirat wants to merge 1 commit intomainfrom
31.-Next-Permutation
Open

Create 31. Next Permutation.md#58
tokuhirat wants to merge 1 commit intomainfrom
31.-Next-Permutation

Conversation

@tokuhirat
Copy link
Owner

This Problem
31. Next Permutation
Next Ploblem
8. String to Integer (atoi)
言語: Python

```python
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
n = len(nums)

Choose a reason for hiding this comment

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

len(nums) のような定数時間でアクセスでき、かつ極めてシンプルなものをあえて変数に置くと却って読みにくくなるかもしれません。

for i in reversed(range(n - 1)):
if nums[i] < nums[i + 1]:
return i
return None

Choose a reason for hiding this comment

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

ここで何を返すかは議論の余地があると思いますが、rfind という名前を使うなら built-in の規則に合わせたくなります。
具体的には -1 を返していますね。

https://docs.python.org/ja/3.13/library/stdtypes.html#str.rfind

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、その観点はありませんでした。個人的には -1 はインデックスとして成立してしまうので None を返したいと思って書きました。いずれにせよdocstringに書いた方が良いですね。ありがとうございます。

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.

2 participants