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;中游水准,自信心受挫。。


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

相关文章推荐

LeetCode89/60 Gray Code/Permutation Sequence--迭代

一:Leetcode 89 Gray Code 题目:The gray code is a binary numeral system where two successive values...

【leetcode】【60】Permutation Sequence

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

<LeetCode OJ> 60. Permutation Sequence

Total Accepted: 56468 Total Submissions: 221337 Difficulty: Medium The set [1,2,3,…,n] contains...

[leetcode]60. Permutation Sequence@Java解题报告

https://leetcode.com/problems/permutation-sequence/description/ The set [1,2,3,…,n] conta...

[LeetCode]problem 60. Permutation Sequence

TAG类·进制转换数学;枚举;找规律 题目链接方法首先,第一眼看过去,没看出来规律。看第二眼,找到了规律: 第高位往低位看,整个随机序列的最高位从1到n的,看某一确定高位下的序列,会发现次高位也是由低...

【一天一道LeetCode】#60. Permutation Sequence.

一天一道LeetCode系列(一)题目 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing ...

LeetCode:60. Permutation Sequence,全排列的第n个子列

LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode: 60. Permutation Sequence 描述: The...

LeetCode | 60. Permutation Sequence——第K个全排列

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

leetcode[60]Permutation Sequence 以及 全排列的编码与解码——康托展开 (附完整代码)

leetcode[60]Permutation Sequence 以及 全排列的编码与解码——康托展开 (附完整代码)...

[Leetcode] 60. Permutation Sequence 解题报告

题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Algorithms—60.Permutation Sequence
举报原因:
原因补充:

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