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.

  分析:数学的思路来做。

class Solution {
public:
string getPermutation(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int A[] = {,, , , , , , , };
string res = "";
vector<bool> flag(n+, false);
for(int i = n-; i >= ; --i)
{
int pos = k / A[i];
if(k%A[i] == && pos > ) --pos;
k = k - pos * A[i];
for(int j = ; j <= n; ++j)
{
if(flag[j] == false){
if(pos == )
{
char c = '' + j;
res += c;
flag[j] = true;
break;
}else{
--pos;
}
}
}
} return res;
}
};

LeetCode_Permutation Sequence的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. 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] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

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

随机推荐

  1. C#中判断字符串是否中文的方法

    public bool IsChinaString(string CString) { bool BoolValue = false; ; i < CString.Length; i++) { ...

  2. [又是BUG]常见的RuntimeException

    妈蛋这异常那异常都是异常,不能忍了! 下面总结一些经常遇到的异常(RuntimeExecption):   算术异常类:ArithmeticExecption 数组下标越界异常:ArrayIndexO ...

  3. 关于我的FGC的OAuth2.0认证。

    这个名字估计很冷门,估计不会有人看到吧,我猜测的. (阅读以下全文之前请先搞定翻^#$%@#墙这件事.昨天看了一个笑话说墙之父方校长说自己有六个VPN账号,只是为了测试自己的墙好用还是VPN好用.哈哈 ...

  4. 判断UserAgent是否为手机

     , )))             {                 return true;             }             return false;         }

  5. HDU_2013——蟠桃记,反向推理

    Problem Description 喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!什么问题?他研究的问题是蟠桃一共有多少 ...

  6. Shell中特殊的变量

    $表示当前的进程,当使用echo $$是会输出当前shell的pid echo $$ 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例 ...

  7. Linux输出重定向

  8. JAVA程序猿怎么才干高速查找到学习资料?

    JAVA程序猿怎么才干高速查找到学习资料? JAVA学习资料在互联网上较为零散,并且大多是英文的.以下介绍3种方式,让程序猿能够高速地找到自己想要的资料. 一.导航站点: 有非常多类似hao123的站 ...

  9. MVC实现类似QQ的网页聊天功能-ajax(下)

    此篇文章主要是对MVC实现类似QQ的网页聊天功能(上)的部分代码的解释. 首先说一下显示框的滚动条置底的问题: 结构很简单一个大的div(高度一定.overflow:auto)包含着两个小的div第一 ...

  10. Linux命令 tar 打包解压缩

    --打包:tartar -cvf 命名文件.tar 要压缩的目录tar -xvf 要解压的包.tartar -tf  要查看的包.tartar -f 包.tar -r file             ...