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 ...
随机推荐
- Nginx的负载均衡和高可用
一.Nginx的理解 Nginx是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是一款轻量级的web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理 ...
- css3动画的原理 及 各种效果制作
1. 制作小球弹动效果 在这篇文章中,我们将会去探究一下浏览器是如何去处理CSS Animations和CSS Transitions的, c 以便使你在写一些动画效果之前就可以对该动画在浏览器中 ...
- 在Outlook中修改脱机文件(.ost)的保存位置
方法一 少读者所在公司的邮箱客户端都在使用微软 Exchange Server 的“缓存 Exchange 模式”.Outlook会默认将脱机文件(.ost文件)保存在C盘上. 但很多读者不希望Out ...
- String..lastIndexOf(".") 返回-1的思考
String s = tableName.substring(tableName.lastIndexOf(".") + 1); 如果有tableName有'.',那么返回正确的截取 ...
- Devexpress TreeList 展开和折叠当前选中节点
1.展开: private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) ...
- Run-time Settings 运行时设置
1.执行顺序设置和执行概率设置 2.迭代之间的等待时间设置 3.日志设置 4.思考时间 5.杂项 线程进程选择: 一般服务器没有安全机制选择线程执行 但是有安全机制的话 比如第一个进程50个线程 第二 ...
- web攻击之一:XSS跨站脚本
一.浏览器安全 同源策略 影响源的因素:host,子域名,端口,协议 a.com通过以下代码: <script scr=http://b.com/b.js> 加载了b.com上的b.js, ...
- mysql5.7.12/13在安装新实例时报错:InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero
.bin/mysqld --initialize-insecure --basedir=xxx --datadir=xxx 然后 .bin/mysqld_safe --defaults-file=xx ...
- 学习笔记之C++ Primer中文版(第五版)
非常权威系统的语言书,正好学习下C++11内容. C++ Primer_百度百科 http://baike.baidu.com/link?url=YLvDJE9w3CjGp3eQwjuXYKUZs7v ...
- 1027代码审计平台 1-sonar scanner
1.代码审计 1.1综合性的代码分析平台 sonar支持自定义规则,较多的公司使用 360火线 1.2IDE辅助功能 Xcode.Android studio 阿里巴巴Java开发手机ide插件支持 ...