Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255914
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-21 16:15:10

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.

Note: Given n will be between 1 and 9 inclusive.




public static String getPermutation(int n, int k) {
//数组赋值
int num[]=new int[n];
   for(int i=0;i     num[i]=i+1;
   if(n==1){
    if(k==1)
    return "1";
    else {
return "";
}
   }
   int left=n-2;
   //遍历k次
   for(int count=1;count     left=n-2;
    //找left 从最后一个起第一个这个值比后一个值小
    while(left>=0&&num[left]>num[left+1])
    left--;
    //不能遍历了
    if(left<0)
    break;
    int mincount=1;
    int right=left;
    int min=num[left];
    //找right值 left后面比left值大的最小值
    for(int i=left;i     if(num[i]>num[left]){
    if(mincount==1){
    right=i;
    min=num[i];
    mincount=2;
    }
    else {
if(num[i] right=i;
    min=num[i];
}

}
   
    }
   
   
    }
    //交换left和right
    int temp=num[left];
    num[left]=num[right];
    num[right]=temp;
    int j=n-1;
    //left后面值倒序
    for(int i=left+1;i     int temp1=num[i];
    num[i]=num[j];
    num[j]=temp1;
    }
   }
   if(left<0)
    return "";
   //当+多的时候使用StringBuffer性能高
   StringBuffer sBuffer=new StringBuffer("");
   for(int i=0;i     sBuffer.append(num[i]);
   return sBuffer.toString();
   
}
阅读(175) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~