31. Next Permutation (JAVA)
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 and use only constant 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
class Solution {
public void nextPermutation(int[] nums) {
int tmp;
int i;
int j;
for(i = nums.length-2; i >= 0; i--){
if(nums[i] < nums[i+1]) break;
} if(i >= 0){
//find the smallest num in the right which is larger than num[i]
for(j = nums.length-1; j >= i; j--){
if(nums[j] > nums[i]) break;
} //swap these two num
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp; //sort
Arrays.sort(nums, i+1, nums.length);
}
else{
Arrays.sort(nums);
}
} }
寻找规律:
- 从右向左扫描,找到小于右侧数字的第一个数字
- 将右侧大于它的最小的那个数放在该位,它和其余右侧的数字从小到大排列放在右侧。
31. Next Permutation (JAVA)的更多相关文章
- 31. Next Permutation (java 字典序生成下一个排列)
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- leetcode 31. Next Permutation JAVA
题目: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数 ...
- 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 ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- 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 ...
随机推荐
- 把网站从 http 转换成 https
基础准备: 一台服务器,一个主域名或多级域名,本次申请的免费 本次环境使用 centos6.5 + nginx1.8 + jdk1.8 + tomcat8 如果需要收费的请参考: 云盾证书服务(包年) ...
- BZOJ 3143 Luogu P3232 [HNOI2013]游走 (DP、高斯消元)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3143 (luogu) https://www.luogu.org/pro ...
- SQL server 从创建数据库到查询数据的简单操作
目录. 创建数据库 创建表 插入数据 查询 1.创建数据库 --创建数据库 create database db_Product go --使用数据库use db_Productgo 2.创建表 -- ...
- 数据库MySQL(课下作业,必做) 20175225
作业要求: 1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功 ...
- a标签点击,页面自动刷新
<a href="javascript:void(0)" id="reDiagnosis" class="checkBtn"oncli ...
- 使用Zabbix通过ILO管理口监控惠普服务器
https://blog.csdn.net/qq_41571056/article/details/82928542
- 在配置em时运到报错ORACLE_UNQNAME not defined
Oracle 11G R2 RAC 配置em时报错 Environment variable ORACLE_UNQNAME not defined. oracle ORACLE_UNQNAME 借楼主 ...
- C#单元测试Nunit小结
1. 下载和安装Nunit工具: NUnit是一款堪与JUnit齐名的开源的回归测试框架,供.net开发人员做单元测试之用,可以从www.nunit.org网站上免费获得,然后在系统中安装: 2. 在 ...
- 快速入门分布式消息队列之 RabbitMQ(3)
目录 目录 前文列表 前言 通道 Channel 一个基本的生产者消费者实现 消费者 生产者 运行结果 应用预取计数 应用 ACK 机制 最后 前文列表 快速入门分布式消息队列之 RabbitMQ(1 ...
- 算法初级(scala)
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum 1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为 ...