본문 바로가기
python-algorithm

leetcode 46. Permutations

by 무적김두칠 2022. 4. 26.

1
2
3
4
5
6
7
from itertools  import permutations
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:        
        ans=[]
        for i in permutations(nums) :
            ans.append(list(i))
        return(ans)
cs

 

1
2
3
4
5
from itertools  import permutations
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:        
        ans=list(map(list, (permutations(nums))))
        return(ans)
cs

위. permutations 안에 개체들이 tuple이라 문제에서 요청하는데로
list 로 type casting 하고 빈 list에 append 하는방법

아래. map 함수 사용

 

메모리나 시간상 차이가 별로 없네용 (머쓱 ^^;)

반응형

댓글