46. Permutations
class Solution:
def permute(self, nums):
def dfs(cur):
# 재귀 탈출 조건
if len(cur) == len(nums):
# cur를 복사해서 붙여줘야함
# 그냥하면 cur를 참조하기 때문에 cur의 변경에 따라서 값이 달라짐
answer.append(cur[:])
return
for i in range(len(nums)):
# 이미 순열에 사용된게아니면 순열에 포함시켜줌
if used[i] == 0:
cur.append(nums[i])
used[i] = 1
dfs(cur)
# 자신 밑에서 다 찾고 다시 자기까지 올라왔으면 자기 자신도 빼줘야함
used[i] = 0
cur.pop()
answer = []
used = [0] * len(nums)
dfs([])
return answer
https://leetcode.com/problems/permutations/Reference
この問題について(46. Permutations), 我々は、より多くの情報をここで見つけました https://velog.io/@oem0404/46.-Permutationsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol