Leetcode_next-permutation(c++ and python version)
2014-05-09 23:58
645 查看
地址:http://oj.leetcode.com/problems/next-permutation/
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.
思路:在讲思路前插个曲。晚上听了大部分worldquant的兼职招聘的宣讲后提前退场回实验室刷题了,但是发现Leetcode挂了,还挂了很久,至少一个半小时,虽然之前遇到好几次leetcode挂了的现象,但是基本几分钟十几分钟就好了。所以无意间发现了这个: http://www.keithschwarz.com/interesting/code/?dir=next-permutation
正好是此题。
大意是:
1. 从数组尾向头找到最大连续升序的序列 x
2. 将序列x起点(数组尾为终点)左边的数(如果有的话),记为y,与该序列中大于y的最小的数交换
3. 将交换后的序列x排序(从小到大)
c++参考代码:
python参考代码:
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.
1,2,3→
1,3,2
3,2,1→
1,2,3
1,1,5→
1,5,1
思路:在讲思路前插个曲。晚上听了大部分worldquant的兼职招聘的宣讲后提前退场回实验室刷题了,但是发现Leetcode挂了,还挂了很久,至少一个半小时,虽然之前遇到好几次leetcode挂了的现象,但是基本几分钟十几分钟就好了。所以无意间发现了这个: http://www.keithschwarz.com/interesting/code/?dir=next-permutation
正好是此题。
大意是:
1. 从数组尾向头找到最大连续升序的序列 x
2. 将序列x起点(数组尾为终点)左边的数(如果有的话),记为y,与该序列中大于y的最小的数交换
3. 将交换后的序列x排序(从小到大)
c++参考代码:
class Solution { public: void nextPermutation(vector<int> &num) { if(num.empty()) return; int pos = num.size()-1; while(pos && num[pos]<=num[pos-1]) --pos; if(!pos) sort(num.begin(), num.end()); else { for(int i = num.size()-1; i>=pos; --i) if(num[i] > num[pos-1]) { swap(num[i], num[pos-1]); break; } sort(num.begin()+pos, num.end()); } } };
python参考代码:
class Solution: # @param num, a list of integer # @return a list of integer def nextPermutation(self, num): if not num: return num for i in range(len(num)-1,0,-1): if num[i] > num[i-1]: for j in range(len(num)-1,i-1,-1): if num[j] > num[i-1]: num[i-1], num[j] = num[j], num[i-1] break return num[:i] + sorted(num[i:]) return sorted(num)
//what's more, it can also abandon sorting at last. What's amazing is the code below is 20ms+ later than the upper one.//I think there must be some optimizations in STL.//coding two weeks later. class Solution {public: void nextPermutation(vector<int> &num) { if(num.empty() || num.size()==1) return; int pos = num.size()-1; for(; pos>=1; --pos) { if(num[pos]>num[pos-1]) break; } if(!pos) { for(int i=0, j=num.size()-1; i<j; ++i,--j) swap(num[i], num[j]); } else { for(int i = num.size()-1; i>=pos; --i) { if(num[i]>num[pos-1]) { swap(num[i], num[pos-1]); break; } } for(int j = num.size()-1; j > pos; --j, ++pos) swap(num[j], num[pos]); return; } }};
//Thrid trialclass Solution {public: void nextPermutation(vector<int> &num) { if(num.empty()) return; int sz = num.size(), pos = 0; for(pos = sz-1; pos>=1; --pos) { if(num[pos]>num[pos-1]) break; } if(!pos) reverse(num.begin(), num.end()); else { for(int i = sz-1; i>=pos; --i) { if(num[i]>num[pos-1]) { swap(num[i], num[pos-1]); break; } } reverse(num.begin()+pos, num.end()); } }};
相关文章推荐
- Leetcode_permutation-sequence(c++ and python version)
- Leetcode_populating-next-right-pointers-in-each-node(updated c++ and python version)
- Leetcode_remove-nth-node-from-end-of-list(c++ and python version)
- Leetcode_first-missing-positive (updated c++ and python version)
- Leetcode_two-sum (c++ and python version)
- **Leetcode_median-of-two-sorted-arrays (c++ and python version)
- Leetcode_path-sum (updated c++ and python version)
- Leetcode_multiply-strings (updated c++ and python version)
- Leetcode_remove-duplicates-from-sorted-list (updated c++ and python version)
- Leetcode_unique-paths (updated c++ and python version)
- Leetcode_binary-tree-zigzag-level-order-traversal (updated c++ and python version)
- Leetcode_max-points-on-a-line(c++ and python version)
- Leetcode_pascals-triangle (updated c++ and python version)
- Leetcode_remove-duplicates-from-sorted-array-ii (c++ and python version)
- Leetcode_wildcard-matching(c++ and python version)
- Leetcode_valid-number(updated c++ and python version)
- Leetcode_best-time-to-buy-and-sell-stock-ii(c++ and python version)
- Leetcode_count-and-say(c++ and python version)
- Leetcode_best-time-to-buy-and-sell-stock(c++ and python version)
- Leetcode_binary-tree-level-order-traversal (updated c++ and python version)