leetCode 60.Permutation Sequence (排列序列) 解题思路和方法

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.

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

思路:这一题还是比較难,暴力全然是找死的,超时没二话。可是数学归纳的方法不是每一个人都能想到,看了非常多资料,也才刚理解了一些思想。

规律:已知n的值,学过排列组合知道共同拥有n!种排列。

第一位每一个数字开头的序列都有(n-1)!

个序列,因此n个数字所以共同拥有n!个序列。

以此类推。第二位每个数开头都有(n-2)!

个序列。

详细代码例如以下:

public class Solution {
    String str = "";
    public String getPermutation(int n, int k) {
    	int[] num = new int[n];
        int[] data = new int[n];//存阶乘的数据
        int i = 0;
        for(; i < n ;i++){
        	num[i] = i+1;
        	if(i == 0)
        		data[i] = 1;
        	else{
        		data[i] = data[i-1]*i;
        	}
        }
        k--;
        while(--i > -1){//循环得到各位数字
        	int k1 = k/data[i];
        	int p = k1+(n-1-i);//数字的位置
        	swap(n-1-i,p,num);
        	if((k = k %data[i]) == 0)//k==0结束
        		break;
        }
        for(int x:num)//得到str
        	str += x;
        return str;
    }
    //将数据插入,后面的依次后移
    public void swap(int i,int j,int[] num)
    {
    	int m = num[j];
        for(int k=j;k>i;k--)
        	num[k]=num[k-1];
        num[i]=m;
    }
}
时间: 2024-10-25 20:43:20

leetCode 60.Permutation Sequence (排列序列) 解题思路和方法的相关文章

【LeetCode每天一题】Permutation Sequence(排列序列)

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 for n = 3: "123" "132" "213" "231" "312" "321&q

Leetcode 60. Permutation Sequence

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): Given n and k, return the kth permutation sequence. Note: Given n will be between

leetcode 60 Permutation Sequence ----- java

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" "3

19.2.9 [LeetCode 60] Permutation Sequence

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 for n = 3: "123" "132" "213" "231" "312" "321&

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i