Algorithms—60.Permutation Sequence

原创 2015年07月09日 20:21:08

思路:自认为想了一个还是比较精巧的方法,因为是只招一个,首先把所有的数字放入list中,取一个删一个。取数的规则为,k-1/每个数字控制的范围。

public class Solution {
    public String getPermutation(int n, int k) {
        int s=1;
        List<Integer> list=new ArrayList<Integer>();
        for (int i = 0; i <n; i++) {
			list.add(i+1);
		}
        for (int i = 1; i <n; i++) {
			s*=i;
		}
        int t=n-1;
        StringBuffer answer=new StringBuffer();
        for (;t!=0;) {
			answer.append(list.get((k-1)/s));
			list.remove((k-1)/s);
			k-=((k-1)/s*s);
			s/=t;
			t--;
		}
        return answer.append(list.get(0)).toString();
    }
}


耗时:260ms;中游水准,自信心受挫。。


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

【leetcode】【60】Permutation Sequence

一、问题描述 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labe...

LeetCode 60. Permutation Sequence

题目 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

[LeetCode]60. Permutation Sequence

60. Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listin...

LeetCode(60) Permutation Sequence

题目The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the ...

leetcode 60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...

60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...

[leetcode] 60.Permutation Sequence

题目: The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of t...

【LeetCode】60. Permutation Sequence

问题描述https://leetcode.com/problems/permutation-sequence/#/descriptionhe set [1,2,3,…,n] contains a to...

LeetCode --- 60. Permutation Sequence

题目链接:Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listi...

leetcode || 60、Permutation Sequence

problem: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)