↓ Skip to main content
  1. Posts/

leetcode 60. Permutation Sequence (求第k个排列)

·326 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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 _k_th permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

思路:还是根据leetcode 31 解题报告 中的算法搞一下就好了。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 15时17分19秒
 4File Name :60.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    void solve(vector<int>&nums)
11    {
12	int n = nums.size();
13	if (n==0) return;
14	int k = -1;
15	for ( int i = n-2 ; i>= 0 ; i--)
16	{
17	    if (nums[i]<nums[i+1])
18	    {
19		k =  i;
20		break;
21	    }
22	}
23	if (k==-1)
24	{
25	    reverse(nums.begin(),nums.end());
26	    return;
27	}
28	int l = -1;
29	for ( int i = n-1 ; i > k ; i--)
30	{
31	    if (nums[k]<nums[i])
32	    {
33		l = i ;
34		break;
35	    }
36	}
37	swap(nums[l],nums[k]);
38	reverse(nums.begin()+k+1,nums.end());
39    }
40    string getPermutation(int n, int k) {
41	vector<int>nums;
42
43	for ( int i = 1 ; i <= n ; i++) nums.push_back(i);
44	for ( int i = 2 ; i <= k ; i++) solve(nums);
45
46	string res = "";
47	int siz = nums.size();
48	for ( int i = 0 ; i < siz ; i++) res = res + char(nums[i]+'0');
49	return res;
50
51    }
52
53};

Related

leetcode 47. Permutations II (生成全排列,有重复元素)

·294 words·1 min
Given a collection of numbers that might contain duplicates, return all possible unique permutations.__ 思路:和leet code 46 类似,最后用set去个重即可。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 15时00分48秒 4File Name :47.cpp 5************************************************ */ 6class Solution { 7 8public: 9 void solve( vector<int>&nums) 10 { 11 int n = nums.size(); 12 if (n==0) return; 13 int k = -1; 14 for ( int i = n-2 ; i >= 0 ; i--) 15 { 16 if (nums[i]<nums[i+1]) 17 { 18 k = i; 19 break; 20 } 21 } 22 if (k==-1) 23 { 24 reverse(nums.begin(),nums.end()); 25 return; 26 } 27 int l = -1; 28 for ( int i = n-1 ; i >k ; i--) 29 { 30 if (nums[k]<nums[i]) 31 { 32 l = i; 33 break; 34 } 35 } 36 swap(nums[l],nums[k]); 37 reverse(nums.begin()+k+1,nums.end()); 38 } 39 40 void pr (vector<int> &nums) 41 { 42 int siz = nums.size(); 43 for ( int i = 0 ; i < siz; i++) 44 printf("%d%c",nums[i],i==siz-1?'\n':' '); 45 } 46 vector<vector<int>> permuteUnique(vector<int>& nums) { 47 set<vector<int> >se; 48 vector<vector<int> >res; 49 int n = nums.size(); 50 int total = 1 ; 51 for ( int i = 2 ; i <= n ; i++) total*=i; 52 53 for ( int i = 1 ; i <= total ; i++) 54 { 55 se.insert(nums); 56// pr(nums); 57 solve(nums); 58 } 59 for ( auto &it :se) 60 { 61 res.push_back(it); 62 } 63 return res; 64 } 65 66};

leetcode 46. Permutations (生成全排列,无重复元素)

·248 words·1 min
Given a collection of distinct numbers, return all possible permutations. 思路:调用n-1次 leetcode 31 解题报告 中提到的算法即可。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 14时49分34秒 4File Name :46.cpp 5 ************************************************ */ 6class Solution { 7 8 public: 9 10 void solve(vector<int> &nums) 11 { 12 int n = nums.size(); 13 if (n==0) return; 14 int k = -1; 15 for ( int i = n-2 ; i >= 0 ; i--) 16 { 17 if (nums[i]<nums[i+1]) 18 { 19 k = i ; 20 break; 21 } 22 } 23 if (k==-1) 24 { 25 reverse(nums.begin(),nums.end()); 26 return; 27 } 28 int l = -1; 29 for ( int i = n-1 ; i > k ; i-- ) 30 { 31 if (nums[k]<nums[i]) 32 { 33 l = i ; 34 break; 35 } 36 } 37 swap(nums[l],nums[k]); 38 reverse(nums.begin()+k+1,nums.end()); 39 } 40 vector<vector<int>> permute(vector<int>& nums) { 41 vector<vector<int> >res; 42 int n = nums.size(); 43 int total = 1; 44 for ( int i = 2 ; i <= n ; i++) total*=i; 45 for ( int i = 1 ; i <= total; i++) 46 { 47 res.push_back(nums); 48 solve(nums); 49 } 50 51 return res; 52 } 53 54};

leetcode 31. Next Permutation (in-place 生成下一个全排列)

·346 words·1 min
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 思路: 参考了wiki_Permutation

leetcode 33. Search in Rotated Sorted Array (无重复数的旋转数组找定值)

·372 words·1 min
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. 思路:找规律。。。二分。。。 0 1 2 3 4 5 6 1 2 3 4 5 6 0 2 3 4 5 6 0 1 3 4 5 6 0 1 2 4 5 6 0 1 2 3 5 6 0 1 2 3 4 6 0 1 2 3 4 5 观察发现。。。a[mid]<a[r]的时候,后半段有序;

leetcode 34. Search for a Range (二分,找到一段值为tar的区间)

·333 words·1 min
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4]. 思路:二分。。。 我好像根本不会二分啊??? 二分查找