leetcode 60:Permutation Sequence

原创 2015年11月18日 21:33:23
题目:

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.

思路:

用回溯的思想来做,实现如下:

具体思路参考Permutation。

class Solution {
public:
	string getPermutation(int n, int k) {
		vector<char> nums;
		for (int i = 1; i <= n; i++) nums.push_back(i+'0');
		count = 0;
		string result;
		back(0, n, k, nums, result);
		return result;
		//return result.substr(0,n);
	}
	void back(int index, int size, int k, vector<char>& nums,string &result)
	{
		if (index >= size)
		{
			count++;
			if (count >= k)	result.assign(nums.begin(),nums.end());
			return;
		}
		for (int i = index; i<size && count<k; i++)
		{
			swap(nums[i], nums[index]);
			back(index + 1, size, k, nums,result);
			swap(nums[i], nums[index]);
		}
	}
private:
	int count;
};
这种解法耗时太长了。

下面用数学解法。

考虑到n个数字,共有n!种组合。nums[]={1,2,...,n},则结果res也有n个数字。

且满足如下关系:res[0]=nums[k/(n-1)!],其他的关系类似。

实现如下:

class Solution {
public:
	string getPermutation(int n, int k) {
		vector<char> nums;
		int factorial = 1;
		string res;
		for (int i = 1; i <= n; i++)
		{
			factorial *= i;
			nums.push_back(i + '0');
		}
		k--;
		for (int i = 0; i < n; i++)
		{
			factorial = factorial / (n - i);
			int select = k / factorial;
			res.push_back(nums[select]);
			for (int j = select; j < n - i - 1; j++)
				nums[j] = nums[j + 1];
			k = k % factorial;
		}
		return res;
	}
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode89/60 Gray Code/Permutation Sequence--迭代

一:Leetcode 89 Gray Code 题目:The gray code is a binary numeral system where two successive values...

【leetcode】【60】Permutation Sequence

一、问题描述 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labe...

<LeetCode OJ> 60. Permutation Sequence

Total Accepted: 56468 Total Submissions: 221337 Difficulty: Medium The set [1,2,3,…,n] contains...

[leetcode]60. Permutation Sequence@Java解题报告

https://leetcode.com/problems/permutation-sequence/description/ The set [1,2,3,…,n] conta...

[LeetCode]problem 60. Permutation Sequence

TAG类·进制转换数学;枚举;找规律 题目链接方法首先,第一眼看过去,没看出来规律。看第二眼,找到了规律: 第高位往低位看,整个随机序列的最高位从1到n的,看某一确定高位下的序列,会发现次高位也是由低...

【一天一道LeetCode】#60. Permutation Sequence.

一天一道LeetCode系列(一)题目 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing ...

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

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

LeetCode | 60. Permutation Sequence——第K个全排列

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

[Leetcode] 60. Permutation Sequence 解题报告

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

LeetCode 60. Permutation Sequence

1. 题目要求 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeli...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 60:Permutation Sequence
举报原因:
原因补充:

(最多只允许输入30个字)