Skip to content

46.Permutations#21

Open
nittoco wants to merge 2 commits intomainfrom
nittoco-patch-21
Open

46.Permutations#21
nittoco wants to merge 2 commits intomainfrom
nittoco-patch-21

Conversation

@nittoco
Copy link
Copy Markdown
Owner

@nittoco nittoco commented Jun 27, 2024

問題文: 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.

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def helper(index):
if(index == 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.

() 書かないほうがいい気がします。

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この for 意味的に 上の for の外にいて欲しいですよね。たぶん、それを表しているのが、この後の必ずする return です。
関数にするほか、

for A:
  if B:
    break
  C
else:
  return None
D

という方法などがあるでしょう。Discord を関数化かなんかで調べると出てくると思います。

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.

https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860
これですね。読んだことあるのに今回考慮に出なかったの反省です。
後でその方法で書いてみます。ありがとうございます

class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def make_next_permutation(permutation):
def find_decreasing_index_from_last(permutation):
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.

これはここまでは関数化しない方が好みということでしょうか。
インデントが深くなりすぎると見にくいからでしょうか。

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.

私は、三重にせず、二重にします。

↑これ意味勘違いしてました。
find_decreasing_index_from_lastを外に出す(make_next_permutationと同じ階層にする)ってことですね。

def permute(self, nums: List[int]) -> List[List[int]]:
def helper(permutation):
if len(permutation) == len(nums):
all_permutations.append(permutation.copy())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

念のためですがpermutation[:]という書き方もできますね。

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.

確かにその選択肢、言われればわかるもののあんまり頭になかったです。ありがとうございます

helper(permutation)
permutation.pop()


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、空行1つでも良いかもです。

@rihib rihib mentioned this pull request Aug 13, 2024
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.

3 participants