zoukankan      html  css  js  c++  java
  • leetcode -- 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         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int len = num.length;
     6         int vioIndex = len - 1;
     7         while(vioIndex > 0){
     8             if(num[vioIndex - 1] < num[vioIndex]){
     9                 break;
    10             }
    11             vioIndex --;
    12         }
    13         
    14         if(vioIndex > 0){
    15             vioIndex --;
    16             int right = len - 1;
    17             while(right >= 0 && num[vioIndex] >= num[right]){
    18                 right --;
    19             }
    20             
    21             int tmp = num[vioIndex];
    22             num[vioIndex] = num[right];
    23             num[right] = tmp;
    24             vioIndex ++;
    25         }
    26         
    27         int end = len - 1;
    28         while(vioIndex < end){
    29             int tmp = num[vioIndex];
    30             num[vioIndex] = num[end];
    31             num[end] = tmp;
    32             vioIndex ++;
    33             end --;
    34         }
    35         
    36         
    37     }
    38 }

    数学太烂!!!

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

     refactor code:

     1 public class Solution {
     2     public void nextPermutation(int[] num) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         int length = num.length;
     5         int index = - 1;
     6         for(int i = length - 1; i >= 1; i--){
     7             if(num[i] > num[i - 1]){
     8                 index = i;
     9                 break;
    10             }
    11         }
    12         
    13         if(index == -1){
    14             reverseArray(num, 0, length - 1);
    15         } else {
    16             int biggerIndex = findBig(num[index - 1], index, num);
    17             swap(num, biggerIndex, index - 1);
    18             reverseArray(num, index, length - 1);
    19         }
    20     }
    21     
    22     public int findBig(int sentinal, int index, int[] num){
    23         int bigIndex = index;
    24         int bigValue = num[index];
    25         for(int i = index + 1; i < num.length; i++){
    26             if(num[i] > num[index - 1] && num[i] <= bigValue){
    27                 bigValue = num[i];
    28                 bigIndex = i;
    29             }
    30         }
    31         return bigIndex;
    32     }
    33     
    34     public void reverseArray(int[] num, int start, int end){
    35         while(start < end){
    36             swap(num, start, end);
    37             start ++;
    38             end --;
    39         }
    40     }
    41     
    42     public void swap(int[] num, int start, int end){
    43         int tmp = num[start];
    44         num[start] = num[end];
    45         num[end] = tmp;
    46     }
    47 }

     重写代码过程中出现的错误:

    1.line:18 反转从partition之后的数字(partition number is inclusive)

    2.line:26 找到比sentinel大的最右侧的数字下标,因为最后需要反转,得到的结果要是最小的,看下例

    Input:[2,3,1,3,3]

    Output:[2,3,3,3,1]

    Expected:[2,3,3,1,3]

  • 相关阅读:
    drop table 、delete table和truncate table的区别
    润乾报表 删除导出excel弹出框里的选项
    学习笔记: 委托解析和封装,事件及应用
    学习笔记: 特性Attribute详解,应用封装
    学习笔记: 反射应用、原理,完成扩展,emit动态代码
    学习笔记: 泛型应用、原理、协变逆变、泛型缓存
    jmeter4.x centos7部署笔记
    rabbitmq3.7.5 centos7 集群部署笔记
    rabbitmq3.8.3 centos7 安装笔记
    UVA-12436 Rip Van Winkle's Code (线段树区间更新)
  • 原文地址:https://www.cnblogs.com/feiling/p/3228736.html
Copyright © 2011-2022 走看看