Created
March 12, 2013 22:26
-
-
Save guolinaileen/5147662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public String getPermutation(int n, int k) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(k<0) return ""; | |
ArrayList<Integer>num=new ArrayList<Integer>(); | |
int sum=1; | |
for(int i=1; i<=n; i++) | |
{ | |
sum*=i; num.add(i); | |
} | |
StringBuffer sb=new StringBuffer(); | |
for(int i=n; i>0; i--) | |
{ | |
sum=sum/i; | |
int idx=(k-1)/sum; | |
sb.append(num.get(idx)); | |
num.remove(idx); | |
k=k-idx*sum; | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment