LeetCode – Permutation Sequence (Java)

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):

"123"
"132"
"213"
"231"
"312"
"321"

Given n and k, return the kth permutation sequence. (Note: Given n will be between 1 and 9 inclusive.)

Java Solution 1

public class Solution {
	public String getPermutation(int n, int k) {
 
		// initialize all numbers
		ArrayList<Integer> numberList = new ArrayList<Integer>();
		for (int i = 1; i <= n; i++) {
			numberList.add(i);
		}
 
		// change k to be index
		k--;
 
		// set factorial of n
		int mod = 1;
		for (int i = 1; i <= n; i++) {
			mod = mod * i;
		}
 
		String result = "";
 
		// find sequence
		for (int i = 0; i < n; i++) {
			mod = mod / (n - i);
			// find the right number(curIndex) of
			int curIndex = k / mod;
			// update k
			k = k % mod;
 
			// get number according to curIndex
			result += numberList.get(curIndex);
			// remove from list
			numberList.remove(curIndex);
		}
 
		return result.toString();
	}
}

Java Solution 2

public class Solution {
	public String getPermutation(int n, int k) {
		boolean[] output = new boolean[n];
		StringBuilder buf = new StringBuilder("");
 
		int[] res = new int[n];
		res[0] = 1;
 
		for (int i = 1; i < n; i++)
			res[i] = res[i - 1] * i;
 
		for (int i = n - 1; i >= 0; i--) {
			int s = 1;
 
			while (k > res[i]) {
				s++;
				k = k - res[i];
			}
 
			for (int j = 0; j < n; j++) {
				if (j + 1 <= s && output[j]) {
					s++;
				}
			}
 
			output[s - 1] = true;
			buf.append(Integer.toString(s));
		}
 
		return buf.toString();
	}
}

6 thoughts on “LeetCode – Permutation Sequence (Java)”

  1. public static String kPerm (int n, int k){

    ArrayList digits = new ArrayList();

    for (int i=1; i<=n; i++) digits.add(i);

    StringBuilder result = new StringBuilder();

    for (int i=0; i=fac){

    digitIndex++;

    k-=fac;

    }

    result.append(digits.remove(digitIndex));

    }

    return result.toString();

    }

  2. public static String kPerm (int n, int k){
    ArrayList digits = new ArrayList();
    for (int i=1; i<=n; i++) digits.add(i);
    StringBuilder result = new StringBuilder();

    for (int i=0; i=fac){
    digitIndex++;
    k-=fac;
    }

    result.append(digits.remove(digitIndex));
    }

    return result.toString();
    }

  3. public static String kPerm (int n, int k){

    ArrayList digits = new ArrayList();
    for (int i=1; i<=n; i++) digits.add(i); //digits = 1,2,3,…,n
    StringBuilder result = new StringBuilder();

    for (int i=0; i=fac){
    digitIndex++; //this digit must be the next largest available digit
    k-=fac;
    } //end while loop

    result.append(digits.remove(digitIndex));
    } //end for loop

    return result.toString();
    }

  4. I have a much simpler solution 🙂
    kth permutation starts at 0

    public static String kPerm (int n, int k){
    ….ArrayList digits = new ArrayList();
    ….for (int i=1; i<=n; i++) digits.add(i); //0,1,2,3,4….n
    ….StringBuilder result = new StringBuilder();

    ….for (int i=0; i=fac){ //we must shift to the next digit to the next largest available digit
    …………….digitIndex++;
    …………….k-=fac;
    ……..}
    ……..result.append(digits.remove(digitIndex));
    ….}
    ….return result.toString();
    }

Leave a Comment