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. 走FILTER效率高的2种情况

    FILTER的适用范围: 1. 主表返回的记录数较少 2.子查询返回记录数较小 下面做实验证明: select department_name from hr.dept_1 dept where de ...

  2. bootstrap栅格布局,第一次成功

    代码: <div class="helper" style="background-color: #F7F7F9;height: 200px;padding-top ...

  3. Linux桌面快捷方式建立方案

    Linux桌面快捷方式建立方案 以下以添加Eclips为例 在桌面上添加Eclips.desktop 文件,向其写入如下代码 [Desktop Entry] Name=Eclipse Comment= ...

  4. FreeMarker---数据类型

    1.a.ftl 你好,${user},今天你的精神不错! ----------------------------- 测试if语句: <#if user=="老高"> ...

  5. EF并发性能文章

    http://www.cnblogs.com/farb/p/ConcurrencyAndTransctionManagement.html

  6. Sequence(组合数学,集合不同元素的个数)

    Sequence [组合数学] 时间限制: 3 Sec  内存限制: 128 MB 提交: 138  解决: 52 [提交][状态][讨论版] 题目描述 在某个夜黑月高的晚上,!!!,原谅我编不下去了 ...

  7. 【C#基础】HTTP发送POST二进制数据

    //postdata为数组的请求方式 public byte[] POST(string Url, byte[] byteRequest) { byte[] responsebody; HttpWeb ...

  8. Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递

    http://niuzhenxin.iteye.com/blog/1706203   Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql.postgresql.. ...

  9. [Polymer] Introduction

    install Polymer and explore creating our first custom element: bower install polymer index.html: < ...

  10. Boost库安装与使用

    Boost 库非常不错,所以我今天就安了它一下下. Boost 库不是 C++ 标准库的一部分(据说在下一版本号的 C++ 标准会採纳它),但它有一些标准库所没有的非常实用的一些功能,比方我非常须要的 ...