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/python/047_Permutations_II.py at master · raghugitrepo/leetcode Expand file tree/ Copy path047_Permutations_II.pyMore file actions 67 lines (63 loc) · 2.06 KB / Copy path047_Permutations_II.pyFile metadata and controls67 lines (63 loc) · 2.06 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 class Solution(object): # def __init__(self): # self.result = {} # # def permuteUnique(self, num): # """ # :type nums: List[int] # :rtype: List[List[int]] # """ # if num is None: # return [] # num.sort() # self.getPermute([], num) # return self.result.values() # # def getPermute(self, prefix, rest): # ls = len(rest) # if ls == 0: # return # elif ls == 1: # temp = prefix + rest # stemp = ''.join(str(t) for t in temp) # self.result[stemp] = temp # else: # for i in range(ls): # if i + 1 < ls and rest[i] == rest[i + 1]: # continue # temp = prefix[:] # temp.append(rest[i]) # self.getPermute(temp, rest[:i] + rest[i + 1:]) def permuteUnique(self, num): res = [] if len(num) == 0: return res self.permute(res, num, 0) return res def permute(self, res, num, index): if index == len(num): res.append(list(num)) return appeared = set() for i in range(index, len(num)): if num[i] in appeared: continue appeared.add(num[i]) num[i], num[index] = num[index], num[i] self.permute(res, num, index + 1) num[i], num[index] = num[index], num[i] def permuteUnique(self, num): # 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. |