GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-3/python/046_Permutations.py at master · vermuz/leetcode-3 Expand file tree/ Copy path046_Permutations.pyMore file actions 44 lines (41 loc) · 1.42 KB / Copy path046_Permutations.pyFile metadata and controls44 lines (41 loc) · 1.42 KB 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 32 33 34 35 36 37 38 39 40 41 42 43 44 class Solution: # import itertools # def permute(self, nums): # """ # :type nums: List[int] # :rtype: List[List[int]] # """ # result = itertools.permutations(nums) # result = [list(t) for t in result] # return result def permute(self, nums): # DPS with swapping res = [] if len(nums) == 0: return res self.get_permute(res, nums, 0) return res def get_permute(self, res, nums, index): if index == len(nums): res.append(list(nums)) return for i in range(index, len(nums)): nums[i], nums[index] = nums[index], nums[i] # s(n) = 1 + s(n-1) self.get_permute(res, nums, index + 1) nums[i], nums[index] = nums[index], nums[i] # def permute(self, nums): # # iterative solution # res = [[]] # for i in range(len(nums)): # cache = set() # while len(res[0]) == i: # curr = res.pop(0) # for j in range(len(curr) + 1): # # generate new n permutations from n -1 permutations # new_perm = curr[:j] + [nums[i]] + curr[j:] # stemp = ''.join(map(str, new_perm)) # if stemp not in cache: # cache.add(stemp) # res.append(new_perm) # return res |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |