Next Permutation
来源:互联网 发布:大米评测淘宝店 编辑:程序博客网 时间:2024/06/07 19:05
题目: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.1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
伪代码:不怎么会写,后面再发把。
具体代码:
void nextPermutation(vector<int>& nums) { int lens = nums.size(); int minIndex = 0; int temp = 0; for (int i = lens - 2; i>=0; i--) { for (int j = lens-1; j>i; j--) { if (nums[j]>nums[i]) { temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; minIndex = i + 1; break; } } if (minIndex) { break; } } for (int i = minIndex; i<lens; i++) { for (int j = i+1; j < lens; j++) { if (nums[j] < nums[i]) { temp = nums[j]; nums[j] = nums[i]; nums[i] = temp; } } }}
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
- javaGUI中 随机生成一组位置不同的按钮 并对JButton的左键、右键、双击的监听
- Full Gc触发条件
- 剑指offer-面试题21:包含min函数的栈
- JavaScript document.write用法
- cmd命令切换目录
- Next Permutation
- CodeForces 628B New Skateboard
- 深入理解JNI
- 【web前端】CSS基础
- 图着色问题
- register关键字的作用
- CheckedTextView使用Demo
- 英语总结
- iOS开发之 cell上label出现文字重叠的情况