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, 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
题意:
产生下一个序列,对给定序列进行重排,生成一个字母顺序比它更大的下一个序列。
如果给定的序列已经是按字母顺序排列中最大的一个了,则进行逆序排列。
算法在数组内进行,不要使用额外空间。
算法设计:
(1)从后向前遍历,找到第一个不满足降序的元素;若初始序列全部是降序,则i为-1,直接跳转至(3);
(2)将该元素同它后面的元素中比它大的第一个元素交换;
(3)将该元素后的所有元素排列,使之成为最小的排列。
代码:
public void nextPermutation(int[] nums) {
if(nums == null || nums.length == 0)
return;
//长度为1的数组
if (nums.length == 1) {
return;
} //从后向前找到第一个不满足逆序的元素
int i = nums.length - 2;
for(; i >= 0 && nums[i] >= nums[i + 1]; --i); //注意,这里有=,可以排除含有重复元素的情况 //从i+1位置开始,向后查找比nums[i]大的最小元素
if(i >= 0){
int j = i + 1;
for(; j < nums.length && nums[j] > nums[i]; ++j);
swap(nums,i,j - 1); //交换,注意是同 j - 1交换
} //将i之后的元素逆置(这里包含一种特殊情况,若该排列已经是字典序中的最大了,则下一个序列应该是最小字典序,因此,直接在这里逆置即可)
int k = nums.length - 1;
i++;
for(; i < k; i++, k--)
swap(nums, i, k);
} /**
* 交换元素
* @param array
* @param i
* @param j
*/
public void swap(int[] array,int i ,int j){
//交换
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
31. Next Permutation (java 字典序生成下一个排列)的更多相关文章
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- POJ:1833 按字典序找到下一个排列:
http://poj.org/problem?id=1833 按照字典的顺序(a-z) (1-9),可以得出任意两个数字串的大小.比如“123”, 最小的是“123”(从小到大),最大的是“321”( ...
- next_permutation():按字典序输出下一个排列
#include<iostream> #include<algorithm> using namespace std; int main() { int data[4]={5, ...
- leetcode 31. Next Permutation(字典序的下一个)
描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- LeetCode:下一个排列【31】
LeetCode:下一个排列[31] 题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排 ...
- SGU 179 Brackets light(生成字典序的下一个序列)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Java实现 LeetCode 31下一个排列
31. 下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...
- LeetCode 31. 下一个排列(Next Permutation)
题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...
随机推荐
- html 之 区块元素属性(待补充)
区块标签:(会随着父类的宽度变化而变化) p,div 等(后续补充) 区块标签才能使用以下属性() width,height,min-height,max-height,min-width,max-h ...
- 论文阅读:Learning Visual Question Answering by Bootstrapping Hard Attention
Learning Visual Question Answering by Bootstrapping Hard Attention Google DeepMind ECCV-2018 2018 ...
- 【Hadoop 分布式部署 十 一: NameNode HA 自动故障转移】
问题描述: 上一篇就是NameNode 的HA 部署完成,但是存在问题,问题是如果 主NameNode的节点宕机了,还是需要人工去使用命令来切换NameNode的Acitve 这样很不方便,所以 ...
- EF Core In-Memory Database Provider
原文链接 This can be useful for testing, although the SQLite provider in in-memory mode may be a more ap ...
- TCP/UDP协议、理解三次握手四次挥手、Socket
一.什么是socket? 中文名叫套接字,是对底层的 TCP IP UDP 等网络协议进行封装,使得上层的应用程序开发者,不用直接接触这对复杂,丑陋的协议. 在程序员的言论,他就是一个封装好的模块,要 ...
- [0412]SQL Server 2008 R2 安装 & 设置
SQL Server 2008 R2 安装 & 设置 Sql Server 安装 安装环境: Windows 10 1709 64位 安装文件: Sql Server 2008 R2 Sql ...
- 【译】第11节---数据注解-TimeStamp
原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...
- WaitingFormHelper
using Lba_Ciac; using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- ip啊
网络模型被OSI分成七层,TCP/IP协议大致对应了2.3.4.7层,分别是数据链路层.网络层.传输层.应用层,IP协议处于网络层上,它的工作原理说白了并不复杂: 整个互联网上所有的机器都有唯一一个I ...
- 【转】Qt鼠标键盘事件
http://blog.csdn.net/lovebird_27/article/details/50351336 Qt 程序需要在main()函数创建一个QCoreApplication对象,然后调 ...