31. Next Permutation Leetcode Python
2015-01-22 06:43
351 查看
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding
outputs are in the right-hand column.
先贴一个不能过的算法,是来自前面的permutaion的解法,用一个used array去标记哪些点用过了。 因为要开辟新空间所以没有过。
用的是bfs 可以标记len(solution)=2时候返回得到的solution[-1]就是所要的permutation
第二个解法来自 http://blog.csdn.net/m6830098/article/details/17291259
但是python里面略有不同,我要先反序找出递增的元素 比如 1234 得到3
然后再反序 找出第一个比3大的数, 二者交换得到1243 再将 124 后面的元素排序。
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding
outputs are in the right-hand column.
1,2,3→
1,3,2
3,2,1→
1,2,3
1,1,5→
1,5,1
先贴一个不能过的算法,是来自前面的permutaion的解法,用一个used array去标记哪些点用过了。 因为要开辟新空间所以没有过。
用的是bfs 可以标记len(solution)=2时候返回得到的solution[-1]就是所要的permutation
class Solution: # @param num, a list of integer # @return a list of integer def findperm(self,num,solution,valuelist,used): if len(solution)==2: return solution elif len(valuelist)==len(num): solution.append(valuelist) for index in range(len(num)): if used[index]==False: valuelist=valuelist+[num[index]] used[index]==True self.findperm(num,solution,valuelist,used) valuelist=valuelist[:len(valuelist)-1] used[index]=False def nextPermutation(self, num): if len(num)<=1: return num used=[False for index in range(len(num))] solution=[] self.findperm(num,solution,[],used) return solution[-1]
第二个解法来自 http://blog.csdn.net/m6830098/article/details/17291259
但是python里面略有不同,我要先反序找出递增的元素 比如 1234 得到3
然后再反序 找出第一个比3大的数, 二者交换得到1243 再将 124 后面的元素排序。
<pre name="code" class="python">class Solution: # @param num, a list of integer # @return a list of integer def nextPermutation(self, num): lenth=len(num)-1 while lenth>0: if num[lenth-1]<num[lenth]: break lenth-=1 if lenth==0: num.reverse() k=len(num)-1 while k>=lenth: if num[k]>num[lenth-1]: break k-=1 num[k],num[lenth-1]=num[lenth-1],num[k] newnum=num[lenth:] newnum.sort() num[lenth:]=newnum return num
相关文章推荐
- python实现 LeetCode31——Next Permutation
- python写算法题:leetcode: 31. Next Permutation
- LeetCode 31 Next Permutation (C,C++,Java,Python)
- [leetcode: Python]31.Next Permutation
- 【LeetCode with Python】 Merge Sorted Array
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Reverse Integer
- 【LeetCode with Python】 Same Tree
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Linked List Cycle
- 【LeetCode with Python】 Pascal's Triangle
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Balanced Binary Tree
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- Python天天美味(31) - python数据结构与算法之插入排序
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Best Time to Buy and Sell Stock II