Leetcode 31. Next Permutation (Medium) (cpp)
来源:互联网 发布:进口美工小号刀片 编辑:程序博客网 时间:2024/05/18 03:39
Leetcode 31. Next Permutation (Medium) (cpp)
Tag: Array
Difficulty: Medium
/*31. Next Permutation (Medium)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,23,2,1 → 1,2,31,1,5 → 1,5,1*/class Solution {public:void nextPermutation(vector<int>& nums) {int i = nums.size() - 1, j = i; for (; i > 0; i--) {if (nums[i] > nums[i - 1]) { break; } }if(i == 0) {sort(nums.begin(), nums.end());return;}for (; j > i; j--) {if (nums[j] > nums[i-1]) break; }swap(nums[i-1], nums[j]);sort(nums.begin() + i, nums.end());}};class Solution {public:void nextPermutation(vector<int>& nums) {next_permutation(nums.begin(), nums.end()); }};
0 0
- Leetcode 31. Next Permutation (Medium) (cpp)
- Leetcode 31. Next Permutation (Medium) (java)
- 31. Next Permutation Medium
- [Leetcode 31, medium] Next Permutation
- Leetcode之Next Permutation (Medium)
- [LeetCode] 031. Next Permutation (Medium) (C++/Python)
- Leetcode 60. Permutation Sequence (Medium) (cpp)
- [LeetCode]31.Next Permutation
- LeetCode --- 31. Next Permutation
- [Leetcode] 31. Next Permutation
- LeetCode 31.Next Permutation
- [leetcode] 31.Next Permutation
- [leetcode] 31. Next Permutation
- *LeetCode 31. Next Permutation
- Leetcode 31. Next Permutation
- leetcode 31. Next Permutation
- 31. Next Permutation LeetCode
- leetcode 31. Next Permutation
- 朴素贝叶斯法
- 回看JSP——include指令与动作、forward动作和param动作
- 同为javascript框架,react和angular各有什么优缺点,各自又适合什么开发场景?
- mysql 索引
- java的泛型和反射,多线程
- Leetcode 31. Next Permutation (Medium) (cpp)
- 应用的导航页
- 【HDOJ】【BestCoder Round #84】1004 Dertouzos
- 页面调度算法----百度2017暑期实习生编程题
- Mindjet MindManager abnormal program termination错误
- Enabling Stack Dumping in Linux Kernel
- Java的注解
- HDU 5546 Ancient Go(DFS)
- curl模拟post请求提交