Level:

  Medium

题目描述:

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

思路分析:

  这道题要求是给出当前序列的下一个序列(当前序列的下一个更大的序列),意思就是求当前序列的下一个字典序列。那么我们有以下算法:

  给定一个序列假如是a1,a2,a3,..ai,ai+1,ai+2..,aj..an

  1.找到最后一个正序序列ai,ai+1;

  2.找到ai后面最后一个比他大的数aj;

  3.交换ai和aj; a1,a2,a3,..aj,ai+1,ai+2..,ai..an

  4.将aj后面的所有数反转,即得到下一个序列,即下一个比它大的数

代码:

public class Solution{
public void nextPermutation(int []nums){
if(nums==null||nums.length==0)
return;
//第一步找到最后一对正序序列
int flag=-1;
for(int i=nums.length-1;i>=1;i--){
if(nums[i]>nums[i-1]){
flag=i-1;
break;
}
}
// 如果不存在正序,则证明该序列是由大到小的逆序,则反转整个序列
if(flag==-1){
reverse(nums,flag+1,nums.length-1);
return;
}
//如果存在正序,则找到flag后面最后一个比它大的数,并交换
for(int j=nums.length-1;j>flag;j--){
if(nums[j]>nums[flag]){
int temp=nums[j];
nums[j]=nums[flag];
nums[flag]=temp;
break;
}
}
//反转flag位置后的序列
reverse(nums,flag+1,nums.length-1);
return ;
}
public void reverse(int []nums,int start,int end){//反转序列
while(start<end){
int temp=nums[end];
nums[end]=nums[start];
nums[start]=temp;
start++;
end--;
}
}
}

27.Next Permutation(下一个字典序列)的更多相关文章

  1. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  2. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  3. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. 2.1.12 Next Permutation 下一个字典序数组

    对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j).如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束:否则,重新对当前排列从后向前扫描,找到第一个大于i的 ...

  7. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

  8. 31. Next Permutation 返回下一个pumutation序列

    [抄题]: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...

  9. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. zookeeper和duboo 没用

    什么是dubbo Dubbo是阿里巴巴SOA服务化治理方案的核心框架,是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 测试和生产公用一套zookeep ...

  2. 启动redis注意事项

    1.需要修改配置文件 redis.conf 三处 a.将bind 127.0.0.0    修改为  bind 0.0.0.0 b.daemonize no      修改为   daemonize ...

  3. 用Redis解决互联网项目的数据读取难点

    Redis在很多方面与其他数据库解决方案不同:它使用内存提供主存储支持,而仅使用硬盘做持久性的存储:它的数据模型非常独特,用的是单线程.另一个大区别在于,你可以在开发环境中使用Redis的功能,但却不 ...

  4. 最小生成树 prim

    1.算法思想: 图采用邻接矩阵存储,贪心找到目前情况下能连上的权值最小的边的另一端点,加入之,直到所有的顶点加入完毕. 2.算法实现步骤: 设图G =(V,E),其生成树的顶点集合为U. (1)把v0 ...

  5. tp5 select回显

    <select name="role_id" id="" class="form-control" required> {vol ...

  6. 巧用渐变色打造精致移动端APP

    渐变色是指某个物体的颜色从明到暗,或由深转浅,或是从一个色彩缓慢过渡到另一个色彩,充满变幻无穷的神秘浪漫气息的颜色.在扁平化设计刚刚兴起时,渐变是设计师们避之不及的设计手法.然而自从Instagram ...

  7. .NET分布式事务处理(转)

    出处:http://www.cnblogs.com/youring2/archive/2011/06/12/MSDTC.html 在进行数据持久化的时候,我们会经常用到事务处理.一般情况下,ADO.N ...

  8. libxml2

    http://www.xmlsoft.org/downloads.html   建议直接用openscenegraph下载的3rdparty:http://www.openscenegraph.org ...

  9. ceph之osd

    一.删除osd ceph osd out 1ceph osd down 1systemctl stop ceph-osd@1ceph osd crush remove osd.1ceph osd rm ...

  10. Solr: a custom Search RequestHandler

    As you know, I've been playing with Solr lately, trying to see how feasible it would be to customize ...