Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 파이썬 릿코드
- 파이썬 알고리즘 풀기
- 파이썬 알고리즘
- 릿코드풀이
- 알고리즘풀이
- 릿코드 풀기
- 파이썬알고리즘풀기
- 파이썬알고리즘
- 상가수익률계산기
- 코틀린기초
- python xor
- python zip_longest
- 릿코드
- python 알고리즘
- python Leetcode
- leetcode풀이
- leetcode 풀기
- 파이썬릿코드풀기
- LeetCode
- binary search
- 릿코드 파이썬
- 알고리즘풀기
- python 릿코드
- 파이썬릿코드
- python sorted
- python priority queue
- 파이썬 프로그래머스
- leetcode풀기
- 릿코드풀기
- 잇츠디모
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 46. Permutations 본문
반응형
46. Permutations
https://leetcode.com/problems/permutations/
Permutations - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1)
처음 문제 접근은 계속 nums의 index를 반복문으로 변경해가면서 풀려고 했더니 문제가 풀리지 않았다.
아래와 같이 그림을 그리면서 생각을 해보니 백트래킹 방식으로 문제를 쉽게 풀 수 있을 것이라는 생각이 들었다.
nums가 [1, 2, 3]을 예시로 설명한 그림이다.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
# backtracking solution
# 결과 리스트
output = []
def recursive(cur, remain):
# termination condition
if len(remain) == 0:
output.append(cur)
return
for i, n in enumerate(remain):
recursive(cur + [n], remain[0:i] + remain[i + 1:])
recursive([], nums)
return output
솔루션2)
python에서는 순열을 생성하는 함수를 제공한다.
permutations() 바로 사용한 솔루션이다.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
return permutations(nums, len(nums))
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 167. Two Sum II - Input Array Is Sorted (0) | 2021.11.12 |
---|---|
LeetCode 풀기 - 283. Move Zeroes (0) | 2021.11.12 |
LeetCode 풀기 - 43. Multiply Strings (0) | 2021.11.10 |
LeetCode 풀기 - 1704. Determine if String Halves Are Alike (0) | 2021.11.10 |
LeetCode 풀기 - 189. Rotate Array (0) | 2021.11.10 |
Comments