[Leetcode] Next Permutation
2014-03-29 13:34
253 查看
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.
搞清排列过程!
字典序排列
把升序的排列(当然,也可以实现为降序)作为当前排列开始,然后依次计算当前排列的下一个字典序排列。
对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j)。如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束;否则,重新对当前排列从后向前扫描,找到第一个大于i的元素k,交换i和k,然后对从j开始到结束的子序列反转,则此时得到的新排列就为下一个字典序排列。这种方式实现得到的所有排列是按字典序有序的,这也是C++ STL算法next_permutation的思想。
如果是找全排列的话,就可以给next_permutation加一个返回值来标记还有没有下一个permutation了,如果没有就反回false。
当然也可以用DFS来做,但是有重复元素的话DFS就有点无能为力了。
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
搞清排列过程!
字典序排列
把升序的排列(当然,也可以实现为降序)作为当前排列开始,然后依次计算当前排列的下一个字典序排列。
对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j)。如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束;否则,重新对当前排列从后向前扫描,找到第一个大于i的元素k,交换i和k,然后对从j开始到结束的子序列反转,则此时得到的新排列就为下一个字典序排列。这种方式实现得到的所有排列是按字典序有序的,这也是C++ STL算法next_permutation的思想。
class Solution { public: void nextPermutation(vector<int> &num) { if (num.size() < 2) return; int i, k; for (i = num.size() - 2; i >= 0; --i) if (num[i] < num[i+1]) break; for (k = num.size() - 1; k > i; --k) if (num[i] < num[k]) break; if (i >= 0) swap(num[i], num[k]); reverse(num.begin() + i + 1, num.end()); } };
如果是找全排列的话,就可以给next_permutation加一个返回值来标记还有没有下一个permutation了,如果没有就反回false。
class Solution { public: bool nextPermute(vector<int> &num) { if (num.size() < 2) return false; int i, k; for (i = num.size() - 2; i >= 0; --i) if (num[i] < num[i+1]) break; for (k = num.size() - 1; k > i; --k) if (num[i] < num[k]) break; if (i >= 0) swap(num[i], num[k]); reverse(num.begin() + i + 1, num.end()); return i >= 0; } vector<vector<int> > permute(vector<int> &num) { vector<vector<int>> res; sort(num.begin(), num.end()); do { res.push_back(num); } while (nextPermute(num)); return res; } };
当然也可以用DFS来做,但是有重复元素的话DFS就有点无能为力了。
class Solution { public: void dfs(vector<vector<int>> &res, vector<int> &path, vector<int> &num, int idx) { if (idx == num.size()) { res.push_back(path); return; } for (auto v : num) if (find(path.begin(), path.end(), v) == path.end()) { path.push_back(v); dfs(res, path, num, idx + 1); path.pop_back(); } } vector<vector<int> > permute(vector<int> &num) { vector<vector<int>> res; vector<int> path; dfs(res, path, num, 0); return res; } };
相关文章推荐
- 轻量级java web实践-10(部署-三方软件)
- Unable to resolve target 'android-17'
- Ubuntu下“E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用)”的解决办法
- Class File Editor: Source not found
- 一元三次方程求和
- 从今天起开始写博客
- C# ref和out
- org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot delete
- mysql表中多timestamp报错#1293
- Servlet的单实例多线程机制
- core标签库
- 利用无线ADB调试代码
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
- CentOS配置telnet
- java中volatile关键字的含义
- 第五周项目二分数类的雏形
- LVS+Keepalived实现高可用集群
- 使用tomcat的问题
- 关于base64编码的原理及实现
- Poj 2187 Beauty Contest_旋转凸包卡壳