【一天一道LeetCode】#31. Next Permutation
来源:互联网 发布:办公软件 英文 编辑:程序博客网 时间:2024/05/24 02:56
一天一道LeetCode系列
(一)题目
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
(二)解题
/*本题用到STL中next_Permutation的思想:(1)首先从右到左找到第一个破坏升序的值a,(2)然后再从右至左找到第一个比a大的数b,(3)交换a和b,(4)再把交换后的a的位置之后的数反转。如1,4,5,3,1第一步找到4,第二步找到5,交换4,5得到15431,然后反转5以后的数,得到15134即为所求。*/class Solution {public: void nextPermutation(vector<int>& nums) { int len = nums.size(); if(len!=1){ int i = len-2; while(i>=0 &&nums[i]>=nums[i+1]) i--; int j =len-1; while(j>i &&nums[j]<=nums[i]) j--; if(i>=0) swap(nums[i],nums[j]); reverse(nums.begin()+i+1,nums.end()); } }};
0 0
- 【一天一道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
- leetcode 31. Next Permutation
- 31. Next Permutation LeetCode
- 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
- 元素设置不同样式时调用哪一个样式问题---(样式权值)
- ViewPager 详解(一)---基本入门
- Java:多线程学习
- 数字三角形
- 使用RecyclerView的丢人史
- 【一天一道LeetCode】#31. Next Permutation
- Assembly(c#中简单说明[转]
- 上机题-进制转换
- AngularJs
- iOS Xcode, 解决“Could not insert new outlet connection”的问题。
- Android4.4起art虚拟机替换dalivk虚拟机
- java编程思想-继承心得
- EMC:ScaleIO、vSAN以及ViPR相互之间的差异
- 支持向量机 - 1 - 准备知识和介绍