31. Next Permutation (Array; Math)
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
思路:对大小的影响尽可能小=>影响尽可能右侧的位=>右向左扫描,找到第一个<右侧的数(i)
将右边最小的>nums[i]的数(j)与它交换
从小到大排列i之后的数
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int size = nums.size();
int tmp, i ,j;
for(i = size-; i >= ; i--){
for(j = i+; j < size; j++){
if(nums[i] >= nums[j]) continue;
//meet the first num < at least one number at right
for(int k = j+; k < size; k++){ //find the smallest one > nums[i]
if(nums[i] >= nums[k] || nums[j] <= nums[k]) continue;
//find the smaller one > nums[i]
j = k;
}
tmp = nums[i];
nums[i]=nums[j];
nums[j]=tmp;
sort(nums.begin()+i+, nums.end());
break;
}
if(j<size) break;
}
if(i<){
sort(nums.begin(), nums.end());
}
}
};
To make codes more simple, we can integrate k iterates into j iterates
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int size = nums.size();
int swapIndex = size, tmp, i ,j;
for(i = size-; i >= ; i--){
for(j = i+; j < size; j++){
if(nums[j] <= nums[i] || (swapIndex<size && nums[j]>=nums[swapIndex])) continue;
swapIndex = j;
}
if(swapIndex>=size) continue;
tmp = nums[i];
nums[i]=nums[swapIndex];
nums[swapIndex]=tmp;
sort(nums.begin()+i+, nums.end());
break;
}
if(i<){
sort(nums.begin(), nums.end());
}
}
};
31. Next Permutation (Array; Math)的更多相关文章
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- 刷题31. Next Permutation
一.题目说明 题目是31. Next Permutation,英文太差看不懂,翻译了一下.才知道是求字典顺序下的下一个排列,不允许使用额外空间.题目难度是Medium! 二.我的实现 首先要进一步理解 ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- javascript - 内置对象 String/Date/Array/Math
1.构建对象的方法 <script> //构建对象方法 //第1种 var people = new Object(); people.name = "iwen"; p ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- java 生成xml文件
这里也使用的是import org.w3c.dom.Document; 首先创建document对象,给该对象赋值,然后将document对象使用transformer的transformer转换方法 ...
- angular的中文文档在这里。。
链接 仿ant-design的 angular组件 echarts文档
- Android Studio利用GitHub托管项目
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- Python PIL : IOError: decoder jpeg not available
The first thing I check when I got this error was to check if libjpeg was installed. Lets try this s ...
- 20155322 2016-2017-2 《Java程序设计》第8周学习总结
20155322 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第八周学习的主要内容是课本的第十四第十五章,主要学习了以下知识点: 了解NIO 会使用Cha ...
- 《DSP using MATLAB》Problem 2.5
2.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...
- LG4777 【模板】扩展中国剩余定理(EXCRT)
题意 题目描述 给定\(n\)组非负整数\(a_i, b_i\),求解关于\(x\)的方程组 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\e ...
- 微信小程序设置底部导航栏目方法
微信小程序底部想要有一个漂亮的导航栏目,不知道怎么制作,于是百度找到了本篇文章,分享给大家. 好了 小程序的头部标题 设置好了,我们来说说底部导航栏是如何实现的. 我们先来看个效果图 这里,我们添加了 ...
- HDU 4586 Play the Dice(数学期望)
Play the Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- [html][javascript]父子窗体传值
父窗体 <script type="text/javascript"> newwindow = window.open("b1.html",&quo ...