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,2
3,2,1
→ 1,2,3
1,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 ...
随机推荐
- 史上最全的maven的pom.xml文件详解(转载)
此文出处:史上最全的maven的pom.xml文件详解——阿豪聊干货 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- Buildroot MariaDB替代MySQL
/********************************************************************************* * Buildroot Maria ...
- mysql单列索引和联合索引的使用
1,首先要确定优化的目标,在什么样的业务场景下,表的大小等等.如果表比较小的话,可能都不需要加索引. 2,哪些字段可以建索引,一般都where.order by 或者 group by 后面的字段. ...
- python list 中元素的统计与排序
1. 用count和dict. dict的存储是散乱的, 不方面打印. 2. 用sorted. 注意, 得到的是一个元组list, 而不再是dict. dict_x = {} for item ...
- INET_ADDRSTRLEN 和 INET6_ADDRSTRLEN 长度
在<netinet/in.h>中有这两个宏的定义 #define INET_ADDRSTRLEN 16 #define INET6_ADDRSTRLEN 46 INET_ADDRSTRLE ...
- QLoo graphql engine 学习二 基本试用(kubernetes)
已经测试过docker&& docker-compose 的运行模式,下面测试下kubernetes的运行模式 kubernetes 我使用docker for mac qloo 安装 ...
- 树莓派系列教程:安装系统与配置环境,使用PuTTy与VNC图形界面远程登录
本文所需物品清单: Raspberry Pi 3 Model B 主板.SD卡与读卡器(用于烧录系统) 资料整理来源在文尾 需要下载的资源与工具: 推荐系统-Raspbian 树莓派官方深度定制的硬件 ...
- application项目获取bean
对于web项目,编程方式获取bean如下: WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); C ...
- 【ZedGraph】右键菜单和鼠标滚轴的移动缩放等功能的启用和禁用 (转)
通过[ZedGraph]控件属性修改: 1.禁用右键菜单: IsShowContextMenu = false; 2.禁用鼠标滚轴移动: IsEnableHPan = false; //禁止横向移动; ...
- 洛谷3343(ZJOI2015)地震后的幻想乡
题目:https://www.luogu.org/problemnew/show/P3343 1.那个时间与边的大小排名有关,所以需要求一下最大边的期望排名就行. 2.期望排名是这样算的:(排名为1的 ...