Open
Conversation
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
oda
reviewed
Jun 29, 2024
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def helper(index): | ||
| if(index == len(nums)): |
oda
reviewed
Jun 29, 2024
Comment on lines
+100
to
+104
| for index in range(i+1, len(next_permute_nums)): | ||
| if next_permute_nums[index] < next_permute_nums[i] or next_permute_nums[index] > bigger_min_value: | ||
| continue | ||
| bigger_min_value = next_permute_nums[index] | ||
| bigger_min_index = index |
There was a problem hiding this comment.
この for 意味的に 上の for の外にいて欲しいですよね。たぶん、それを表しているのが、この後の必ずする return です。
関数にするほか、
for A:
if B:
break
C
else:
return None
Dという方法などがあるでしょう。Discord を関数化かなんかで調べると出てくると思います。
Owner
Author
There was a problem hiding this comment.
https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860
これですね。読んだことあるのに今回考慮に出なかったの反省です。
後でその方法で書いてみます。ありがとうございます
oda
reviewed
Jun 29, 2024
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def make_next_permutation(permutation): | ||
| def find_decreasing_index_from_last(permutation): |
Owner
Author
There was a problem hiding this comment.
これはここまでは関数化しない方が好みということでしょうか。
インデントが深くなりすぎると見にくいからでしょうか。
Owner
Author
There was a problem hiding this comment.
私は、三重にせず、二重にします。
↑これ意味勘違いしてました。
find_decreasing_index_from_lastを外に出す(make_next_permutationと同じ階層にする)ってことですね。
Mike0121
reviewed
Jun 30, 2024
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def helper(permutation): | ||
| if len(permutation) == len(nums): | ||
| all_permutations.append(permutation.copy()) |
Owner
Author
There was a problem hiding this comment.
確かにその選択肢、言われればわかるもののあんまり頭になかったです。ありがとうございます
Mike0121
reviewed
Jun 30, 2024
| helper(permutation) | ||
| permutation.pop() | ||
|
|
||
|
|
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.
問題文: https://leetcode.com/problems/permutations/description/
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.