A C# Solution by calculating sequence


  • 0
    L
    public string GetPermutation(int n, int k) {
        int total = 1;
        IList<int> list = new List<int>();
        list.Add(0);
        for(int i = 1; i <= n; i++)
        { list.Add(i); total *= i; }
        if(k > total || k < 1) return null;
        StringBuilder sb = new StringBuilder();
        while(k != 0){
            total /= n--;
            int num = k / total;
            if((k %= total) != 0) num++;
            sb.Append(list[num]);
            list.RemoveAt(num);
        }
        for(int i = list.Count - 1; i >= 1; i--) sb.Append(list[i]);
        return sb.ToString();
    }

Log in to reply
 

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