zoukankan      html  css  js  c++  java
  • Permutation Sequence

    1. Title

    Permutation Sequence

    2.   Http address

    https://leetcode.com/problems/permutation-sequence/

    3. The question

    The set [1,2,3,…,n] contains a total of n! unique permutations.

    By listing and labeling all of the permutations in order,
    We get the following sequence (ie, for n = 3):

    1. "123"
    2. "132"
    3. "213"
    4. "231"
    5. "312"
    6. "321"

    Given n and k, return the kth permutation sequence.

    4. My code (AC)

     1     void modifyMin(char min[],int index)
     2     {
     3         for(int i = index; i < min.length - 1; i++)
     4         {
     5             min[i] = min[i+1];
     6         }
     7     }
     8     public String getPermutation(int n, int k) {
     9 
    10             char str [] = new char[n];
    11             char min [] = new char[n];
    12             int base = 1;
    13             int layer = n-1;
    14             //base = (n-1)!
    15             min[0] = ( char ) (1 + '0');
    16             for(int i = 1 ; i <n ; i++)
    17             {
    18                 base *= i;
    19                 min[i] = (char )(i + '1');
    20             }
    21             for(int i = 0 ; i < n; i++)
    22             {
    23                 int index = k / base;
    24                 if ( k % base == 0)
    25                     index --;
    26             
    27                 // the min permutation
    28                 if( k == index * base + 1)
    29                 {
    30                     str[i] = min[index];
    31                     modifyMin(min,index);
    32                     int jj = 0;
    33                     for(int j = i + 1; j < n; j++)
    34                     {
    35                         str[j] = min[jj++];
    36                     }
    37                     return new String(str);
    38                 }
    39 
    40                 // the max permutation
    41                 if ( k == (index + 1) *base ) {
    42                     str[i] = min[index];
    43                     modifyMin(min,index);
    44                     for(int j = i + 1; j < n; j++)
    45                     {
    46                         str[j] = min[n-j -1];
    47                     }
    48 
    49                     return new String(str);
    50                 }
    51                 
    52                 str[i] = min[index];
    53                 modifyMin(min,index);
    54                 k -= index * base;
    55             
    56                 if (layer >= 2)
    57                 {
    58                     base /= layer;
    59                     layer--;
    60                 }else{
    61                     base = 1;
    62                 }
    63             }
    64             return new String(str);
    65     }
  • 相关阅读:
    10000000000
    vue生命周期
    react基础
    第一个react
    vuex状态管理2
    vue配合UI组件
    vuex
    vue-router配合vue-cli的实例
    vue-router2.0
    父子组件2.0
  • 原文地址:https://www.cnblogs.com/ordili/p/4928488.html
Copyright © 2011-2022 走看看