Skip to content

Create 46. Permutaions.md#5

Open
mt2324 wants to merge 1 commit into
mainfrom
mt2324-patch-2
Open

Create 46. Permutaions.md#5
mt2324 wants to merge 1 commit into
mainfrom
mt2324-patch-2

Conversation

@mt2324

@mt2324 mt2324 commented Jun 23, 2026

Copy link
Copy Markdown
Owner

STEP 0
2ヶ月前ぐらいにArai60やGlind75などの問題をざっと100問程度解いて基本的な解法を頭に入れた時のコード。
STEP1-3は協会準拠

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2:

Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:

Input: nums = [1]
Output: [[1]]

Constraints:

1 <= nums.length <= 6
-10 <= nums[i] <= 10
All the integers of nums are unique.

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:

Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:

Input: nums = [1]
Output: [[1]]
 
Constraints:

1 <= nums.length <= 6
-10 <= nums[i] <= 10
All the integers of nums are unique.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
all_permutations = []
length = len(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.

lengthlen(nums) の値を代入していますが、length だと「何のlengthだっけ?」となるので自分なら len(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.

ここは自分でもどうするか迷いましたし、何回も使うわけではないので確かにlen(nums)のままの方がいいかもしれないですね。


## STEP 2
変数の名前をちょっと見直した。
他の人のコードを見ると存在確認したいものが静的な場合特にused = [False] * len(nums)使うと良さそう。動的に増える場合でもsetだと拡張する時にリハッシュするのに時間かかるから変化の程度によっては動的でもused = [False] * len(nums)としてもあんまり変わんないかもしれない。要素がuniqueでなければsetは使えないし。

Copy link
Copy Markdown

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.

2 participants