【leetcode】 Permutation Sequence (middle)
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.
思路:给定序号找排列的字符串,肯定不用一个一个求,根据序号来判断每一位上的数字。用一个向量存储 0~n-1的阶乘,用另一个向量vec从小到大存1~n数字, 求第k位的话,我们用k-1(转为从0开始), 除以(n-1)! 其整数部分就是该位数字在vec的序号。之后在vec中删掉该数字,k2 %= (n-1)! 以此类推
class Solution {
public:
string getPermutation(int n, int k) {
vector<int> factorial(n, );
vector<int> vec(n, );
string ans;
for(int i = ; i < n; i++)
{
factorial[i] = factorial[i - ] * i;
vec[i] = i + ;
}
if(k > factorial[n - ] * n)
return ans; int k2 = k - ;
for(int i = n - ; i >= ; i--)
{
int cur = k2 / factorial[i];
char c[];
c[] = '' + vec[cur];
c[] = '\0';
ans.append(c);
vec.erase(vec.begin() + cur);
k2 = k2 % factorial[i];
}
return ans;
}
};
【leetcode】 Permutation Sequence (middle)的更多相关文章
- 【Leetcode】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【leetcode】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【LeetCode】Permutation全排列
1. Next Permutation 实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合.若其已经是最大排列,则返回最小排列 ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- [设计模式] javascript 之 策略模式
策略模式说明 定义: 封装一系列的算法,使得他们之间可以相互替换,本模式使用算法独立于使用它的客户的变化. 说明:策略模式,是一种组织算法的模式,核心不在于算法,而在于组织一系列的算法,并且如何去使用 ...
- IE打开报错,提示该内存不能为read的解决办法!
由于最近我遇到过一次浏览器打不开的情况,出错的错误提示为 浏览器错误:“0x5ddfddac”指令引用的“0x00000020”内存,该内存不能为read经过杀毒及IE修复均不能解决!(没试过360急 ...
- spark
http://www.cnblogs.com/shishanyuan/p/4723604.html?utm_source=tuicool spark presto2.0计算引擎 http://blog ...
- Ubuntu 14 安装 “宋体,微软雅黑,WPS Office的symbol、wingdings、wingdings 2、wingdings 3、webding字体,Consolas雅黑混合版编程字体” 等 Windows 7 下的字体
Windows平台下,“宋体”.“微软雅黑”.“Courier New(编程字体)”用的比较多,看的也习惯了.那如何在 Ubuntu下也安装这些字体呢? 操作步骤如下: 第一步:从 Windows 7 ...
- 在Xcode6.4中使用OpenCV
XCode版本6.4,OpenCV版本3.0.0 昨天我安装完OpenCV之后,兴奋地按照这篇文章Mac平台上OpenCV开发环境搭建的步骤,在XCode上建了一个Demo工程,结果编译一直不成功.一 ...
- 读w3c中文教程对键盘事件解释的感想 -遁地龙卷风
写这篇博文源于w3c中文教程对键盘事件的解释, onkeydown 某个键盘按键被按下 onkeypress 某个键盘按键被按下并松开 onkeyup 某个键盘按键被松开 可在实践中发现 只注册key ...
- BZOJ4514——[Sdoi2016]数字配对
有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci×cj 的 ...
- 我对自己提的几个关于cocos2dx的几个问题
1.友元函数的定义: 2.运算符重载: 3.内存关机机制: 4.动作侦听: 5.单点触摸: 6.触摸目标判断: 7.事件传递: 8.多点触摸: 9.加速传感器: 10.物理按键交互: 11.绘图API ...
- 技术博客(初用markdown)。
技术博客 菜鸟教程在这个网站我学到许多有趣的东西,并且弥补了我之前的一些不足之处. 以下为我学习到的内容 输出不同的三位数 以下为代码和输出结果 *** #include<stdio.h> ...
- 剑指Offer 包含min函数的栈
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 思路: 这个题是想得到一个时间复杂度为O(1)的min函数,所以应用一个辅助栈,压的时候,如果A栈的压入比B栈压入 ...