Create 46. Permutaions.md#5
Open
mt2324 wants to merge 1 commit into
Open
Conversation
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.
skypenguins
reviewed
Jun 23, 2026
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| all_permutations = [] | ||
| length = len(nums) |
There was a problem hiding this comment.
length に len(nums) の値を代入していますが、length だと「何のlengthだっけ?」となるので自分なら len(nums) のままにします。
Owner
Author
There was a problem hiding this comment.
ここは自分でもどうするか迷いましたし、何回も使うわけではないので確かにlen(nums)のままの方がいいかもしれないですね。
skypenguins
reviewed
Jun 23, 2026
|
|
||
| ## STEP 2 | ||
| 変数の名前をちょっと見直した。 | ||
| 他の人のコードを見ると存在確認したいものが静的な場合特にused = [False] * len(nums)使うと良さそう。動的に増える場合でもsetだと拡張する時にリハッシュするのに時間かかるから変化の程度によっては動的でもused = [False] * len(nums)としてもあんまり変わんないかもしれない。要素がuniqueでなければsetは使えないし。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.