Permutations II @ LeetCode (Python)
kitt
posted @ 2014年2月22日 20:44
in LeetCode
, 3187 阅读
和Permutation那道题目差不多,也是递归,不过要去重。先排序,再利用prevNum变量区分是否重复。
class Solution: # @param num, a list of integer # @return a list of lists of integers def permuteUnique(self, num): length = len(num) if length == 0: return [] if length == 1: return [num] num.sort() res = [] prevNum = None for curr in xrange(length): if num[curr] == prevNum: continue prevNum = num[curr] for j in self.permuteUnique(num[:curr] + num[curr + 1:]): res.append([num[curr]] + j) return res