【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 ...
随机推荐
- sql拼音简写函数
USE [HotelDB]GO /****** Object: UserDefinedFunction [dbo].[fn_GetPy] Script Date: 2016/1/4 13:29:13 ...
- netbeans tomcat
http://www.cnblogs.com/fengzy/archive/2013/05/18/3086371.html http://blog.csdn.net/xumengxing/articl ...
- ajax异步提交数据动态更改select选项
<!DOCTYPE html> <html> <head> <title></title> <script src="../ ...
- 学C++50条建议
1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programm ...
- 跟着百度学PHP[3]-PHP中结构嵌套之循环结构与条件结构嵌套
任务 有个学生数组存储了学号和姓名,我们需要查找学号为"2014"的学生姓名,这时候我们就需要遍历该数组,并判定学号是否为"201 <?php $student = ...
- 两轮自平衡小车双闭环PID控制设计
两轮自平衡小车的研究意义 ...
- 2016年11月14日--SQL创建数据库、表-查、插、删、改
--创建数据库(create database 数据库名)create database hq20161114go --使用选择数据库(use 数据库名)use hq20161114go --创建学生 ...
- Hello 2016
Hello 2016 I am really happy to work and study here. Nothing is better than be oneself ! It's import ...
- Webclent基本操作
/** * @Title: webclientTest.java * @Package webclient * @Description: TODO(用一句话描述该文件做什么) * @author A ...
- 解析JSON插入数据库
<?php header("Content-Type:text/html;charset=utf-8"); include_once('./mysql.php'); $fil ...