permutation
字典序1,2,3—>1,3,2—>2,1,3—>2,3,1—>3,1,2—>3,2,1—>1,2,3
1.Next Permutation
寻找下一个序列的过程,首先找到第一个非降序的point,之后将该point与后续第一个大于该数的point对换,之后将后续数组逆序。
n = len(nums) - 1
m = n
while m:
if nums[m] > nums[m - 1]:
break
m -= 1
if not m:
nums.sort()
#index = nums[m - 1]
else :
index = nums[m - 1]
l = n
while l:
if nums[l] > index:
break
l -= 1
nums[m - 1], nums[l] = nums[l], nums[m - 1]
nums[m:n+1] = nums[m:n+1][::-1]
2.permute:
递归算法,在任意位插入新point
return nums and [node[:i] + [nums[0]] + node[i:]
for node in self.permute(nums[1:])
for i in range(len(nums))] or [[]]
3.permute 消除重复
以重复数字的第一次出现为标记,不往后插
res = [[]]
for p in nums:
res_n = []
for node in res:
for i in xrange(len(node)+1):
res_n.append(node[:i]+[p]+node[i:])
if i < len(node) and node[i] == p :
break
res = res_n
return res