Next Permutation
来源:互联网 发布:linux snmp测试 编辑:程序博客网 时间:2024/05/29 18:59
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
class Solution {public: void nextPermutation(vector<int> &num) { int n = num.size(); int i, j, tmp; if(n < 2) return; for(i = n - 2; i >= 0; i--) { if(num[i] < num[i + 1]) break; } for(j = n - 1; j > i; j--) { if(num[j] > num[i]) break; } if(i >= 0) { tmp = num[i]; num[i] = num[j]; num[j] = tmp; reverse(num.begin() + i + 1, num.end()); } else reverse(num.begin(), num.end()); }};
0 0
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- ActionBar的详解
- 集成融云报错duplicate symbol
- [Unity]内购插件Unibill
- Construct Binary Tree from Inorder and Postorder Traversal --- LeetCode
- C程序模板
- Next Permutation
- 第三篇 设计模式--抽象工厂模式
- 关于iPhone文件系统NSFileManager讲解
- Redis 数据类型值 list
- qq空间营销快速排名技巧
- 根据时间搜索数据
- SQL SERVER与oracle中的递归处理
- 脚本式计算能力对报表工具的重要性
- IOS中Json解析的四种方法