LeetCode (Next Permutation)
来源:互联网 发布:q币可以在淘宝买东西吗 编辑:程序博客网 时间:2024/06/03 09:24
Problem:
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>& nums) { if (!nums.empty() && nums.size() != 1){ int i = nums.size() - 2; while (i >= 0){ int k = 0; int j = nums.size() - 1; while (j > i){ if (nums[j] > nums[i]){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; reverse(nums.begin() + i + 1, nums.end()); k = 1; break; } else j--; } if (k == 1) break; else if(i != 0) i--; else{ reverse(nums.begin(), nums.end()); break; } } } }};
0 0
- LeetCode: Next Permutation
- LeetCode Next Permutation
- LeetCode : Next Permutation
- [Leetcode] Next Permutation
- [LeetCode] Next Permutation
- [Leetcode] Next Permutation
- leetcode 88: Next Permutation
- [LeetCode]Next Permutation
- LeetCode-Next Permutation
- [leetcode]Next Permutation
- LeetCode - Next Permutation
- LeetCode 31: Next Permutation
- 【leetcode】Next Permutation
- LeetCode题解:Next Permutation
- 【LeetCode】Next Permutation
- [LeetCode] Next Permutation
- LeetCode Next Permutation
- leetCode | Next Permutation
- 关于RecyclerView实现瀑布流,上下滑动时item之间互换位置的问题
- Tomcat与Java Web开发的思维导图
- “玲珑杯”郑州轻工业学院第九届ACM程序设计大赛圆满结束
- #pragma once 与 #ifndef...#define...#endif的比较
- 脚本作业
- LeetCode (Next Permutation)
- [SMOJ1829]公司招聘
- CNN Reranking Question-Answer Pairs 句子矩阵宽卷积和窄卷积方法
- vue.js学习笔记--遇到的那些问题
- Android studio Error:(1, 1) 错误: 非法字符: '\ufeff' 解决方案
- mysql基本操作
- ASP.NET 在IIS操作WORD文档出错的解决办法
- Java NIO之Selector
- MFC程序的执行顺序