next-permutation

package com.ytx.array;
/**
 *  题目 :   next-permutation
 * 
 *  描述:    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
                           
                           
 * @author yuantian xin
 *
 *     题意就是说按照字典序(升序)生成当前排列的下一个排列,如果输入序列已经是降序排列,说明这个是最后一个排列,直接反序输出即可。
 *  1234->1243->1324->1342->1423->1432->4123->...->4321->1234;
 *
 */
public class Next_permutation {
       
       
       public void nextPermutation(int[] num) {
             int len = num.length;
        //从后往前找到第一个升序位置
             int pos = -1;
             for(int i = len - 1; i > 0; i--) {
                    if(num[i] > num[i-1]) {
                           pos = i - 1;
                           break;
                    }
             }
             
             //如果不存在升序,说明这个序列是最后一个排列,直接逆序这个序列并return。
             if(pos < 0) {
                    reverse(num, 0, len-1);
                    return;
             }
             
             //如果找到了升序位置,那么从后往前找,找到pos之后第一个比num[pos]大的位置,并且交换这两个位置上的数。
             for(int i = len-1; i > pos; i--) {
                    if(num[i] > num[pos]) {
                           int temp = num[i];
                           num[i] = num[pos];
                           num[pos] = temp;
                           break;
                    }
             }
             //逆序pos位置之后的数字
             reverse(num, pos + 1, len-1);
             
    }  
   
       public void reverse(int[] num, int begin, int end) {
             
             int temp;
             
             while(begin < end) {
                    temp = num[begin];
                    num[begin] = num[end];
                    num[end] = temp;
                    begin++;
                    end--;
             }
             
       }
       public static void main(String[] args) {
             /*int [] num = {1,2,3,6,5,4};*/
             int [] num = {1,2,3,4,6,5};
             new Next_permutation().nextPermutation(num);
             for(int i = 0; i < num.length; i++) {
                    System.out.print(num[i] + " ");
             }
       }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值