zoukankan      html  css  js  c++  java
  • 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

    [解题思路]
    这题更像一道数学题,画了个图表示算法,如下:

     
     
     1 public class Solution {
     2         public  void  nextPermutation(int[] num) {
     3             int len = num.length;
     4             int pIndex = -1;
     5             int changeIndex = -1;
     6             for(int i = len-1; i > 0; i--){
     7                 if(num[i-1] < num[i]){
     8                     pIndex = i-1;
     9                     break;
    10                 }
    11             }
    12             
    13             if(pIndex == -1){
    14                 reverseArray(num, 0, len-1);
    15             }else{
    16                 for(int i = len-1; i>= 0; i--){
    17                     if(num[i] > num[pIndex]){
    18                         changeIndex = i;
    19                         break;
    20                     }
    21                 }
    22                 swap(num, pIndex, changeIndex);
    23                 reverseArray(num, pIndex+1, len-1);
    24             }
    25         }
    26         
    27         private void reverseArray(int[] num, int start, int end){
    28             while(start <= end){
    29                 swap(num, start, end);
    30                 start++;
    31                 end--;
    32             }
    33         }
    34         
    35         private void swap(int[] num, int a, int b){
    36             int tmp = num[a];
    37             num[a] = num[b];
    38             num[b] = tmp;
    39         }
    40 }        
     
    public class Solution {
        
            public  void  nextPermutation(int[] num) {
                // Note: The Solution object is instantiated only once and is reused by each test case.
                int length = num.length;
                int index = - 1;
                
                /*  from right to left, find the first digit which violate the increase trend. 
                /*call this digit partition number
                */
                for(int i = length - 1; i >= 1; i--){
                    if(num[i] > num[i - 1]){
                        index = i;
                        break;
                    }
                }
                
                // if no digit violate the increase trend. reverse the digit
                if(index == -1){
                    reverseArray(num, 0, length - 1);
                } 
                
                /* from right to left. find the first digit larger than the partition number
                *  call this digit change number. swap it with partition number
                * reverse all the digit on the right of partition index
                */
                else {
                    int biggerIndex = findBig(num[index - 1], index, num);
                    swap(num, biggerIndex, index - 1);
                    reverseArray(num, index, length - 1);
                }
            }
            
            public   int findBig(int sentinal, int index, int[] num){
    //            int bigIndex = index;
    //            int bigValue = num[index];
    //            for(int i = index + 1; i < num.length; i++){
    //                if(num[i] > num[index - 1] && num[i] <= bigValue){
    //                    bigValue = num[i];
    //                    bigIndex = i;
    //                }
    //            }
    //            return bigIndex;
                int changeIndex = 0;
                int changeVaule;
                for(int i = num.length-1;i>=0;i--){
                    if(num[i] > sentinal){
                        changeIndex = i;
                        changeVaule = num[i];
                        break;
                    }
                }
                return changeIndex;
            }
            
            public  void reverseArray(int[] num, int start, int end){
                while(start < end){
                    swap(num, start, end);
                    start ++;
                    end --;
                }
            }
            
            public  void swap(int[] num, int start, int end){
                int tmp = num[start];
                num[start] = num[end];
                num[end] = tmp;
            }
            
           
        
    }

    ref:http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html 

  • 相关阅读:
    原来发布到主页是发布到博客园的主页啊,还真不知道
    sysobjects
    blend3破解文件,直接覆盖安装目录源文件即可。
    Microsoft Sql中char和varchar的区别与联系
    SQL脚本创建数据表,并给出主键,外键,唯一键,约束
    asp.net调试方法
    VS2010安装项目的系统必备中添加.NET 2.0
    c#,自定义安装,部署,创建自定义操作,自定义操作,安装验证,数据库,安装授权码,接收输入,判断
    Ext2.2系列(50篇)
    C# 获取24小时制的当前系统时间
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3533660.html
Copyright © 2011-2022 走看看