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

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

Solution:

 public class Solution {
public String getPermutation(int n, int k) {
if(n==1&&k==1)
return "1";
int total=getTotal(n);
String original=getOriginal(n);
char[] result=new char[n];
for(int i=0;i<n;++i){
total/=(n-i);
int t2=(k-1)/total;
result[i]=original.charAt(t2);
original=original.replace(result[i]+"", ""); //这个方法很巧妙啊,用此法就可以把用过的数字从数组里去掉了!!!
k-=t2*total;
}
return new String(result);
} private String getOriginal(int n) {
// TODO Auto-generated method stub
String result="";
for(int i=1;i<=n;++i){
result+=i+"";
}
return result;
} private int getTotal(int n) {
// TODO Auto-generated method stub
int total=1;
for(int i=1;i<=n;++i){
total*=i;
}
return total;
}
}

[Leetcode] Permutation Sequence的更多相关文章

  1. [LeetCode] Permutation Sequence 序列排序

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

  2. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

  3. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  4. LeetCode——Permutation Sequence

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

  5. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  7. Java for LeetCode 060 Permutation Sequence

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

  8. 【LeetCode练习题】Permutation Sequence

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

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

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

随机推荐

  1. .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>

    无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...

  2. MySQL 主从同步

    http://blog.csdn.net/z69183787/article/details/53897894

  3. Shell编程基础教程1--Shell简介

    1.Shell简介 1.1.查看你系统shell信息 cat /etc/shell 命令可以获取Linux系统里面有多少种shell程序 echo $SHELL 命令可以查看当前你所使用的shell是 ...

  4. java中文乱码解决方法汇总

    public static void main(String[] argv){ try {                 System.out.println(“中文”);//1           ...

  5. GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件

    GMap.NET是什么? 来看看它的官方说明:GMap.NET is great and Powerful, Free, cross platform, open source .NET contro ...

  6. SSAS Cube 维度成员关系Rigid 和 Flexible

    维度成员关系指示成员关系是否随时间而更改.  值为 Rigid 和 Flexible,前者表示成员之间的关系不随时间而更改,后者表示成员之间的关系随时间而更改. 默认值为 Flexible.  指定适 ...

  7. 进程控制理论,王明学learn

    进程控制理论 一.进程 进程是一个具有一定独立功能程序的一次运行活动. 1.1进程特点 1.动态性:进程的实质是程序的一次执行过程,进程是动态产生,动态消亡的 2.并发性:任何进程都可以同其他进程一起 ...

  8. 【J2EE入门】13个规范

    看过了j2ee教学视频,真的让我痛彻心扉,那叫痛并快乐着,痛是因为看了这么长时间,自己只知道了13个规范的概念:同样,快乐也正是因为我知道了13个规范的概念,接下来就是逐项实践的学习. 看了教学视频, ...

  9. AchartEngine的柱状图属性设置

    1.      修改背景色或设置背景图片 背景色设置需要设置两项:setMarginsColor(设置四边颜色)以及setBackgroundColor(设置中间背景色) 设置背景图片:        ...

  10. java多线程--实现Runnable接口

    package unit8; import java.applet.Applet; import java.awt.Label; import java.awt.TextField; public c ...