Clean JAVA Solution


  • 0
    A
    public String getPermutation(int n, int k) {
    	List<Integer> list = new ArrayList<Integer>();
    	for (int i = 1; i <= n; i++) list.add(i);
    
    	StringBuilder sb = new StringBuilder();
    	k--;
    	while (list.size() > 0) {
    		n = list.size();
    		sb.append(String.valueOf(list.remove(k / fact(n - 1))));
    		k = k - ((k / fact(n - 1)) * fact(n - 1));
    	}
    	return sb.toString();
    }
    
    int fact(int x) {
    	return (x == 0 || x == 1) ? 1 : x * fact(x - 1);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.