leetCode 60.Permutation Sequence (排列序列) 解题思路和方法
来源:互联网 发布:炉石传说激活码淘宝买 编辑:程序博客网 时间:2024/05/23 00:10
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.
思路:这一题还是比较难,暴力完全是找死的,超时没二话。但是数学归纳的方法不是每个人都能想到,看了很多资料,也才刚理解了一些思想。
规律:已知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; }}
0 0
- leetCode 60.Permutation Sequence (排列序列) 解题思路和方法
- LeetCode 60. Permutation Sequence(排列序列)
- leetcode之Permutation Sequence解题思路
- Leetcode刷题记—— 60. Permutation Sequence(排列序列)
- Leetcode #60. Permutation Sequence 排列组合序列 解题报告
- Permutation Sequence 排列序列
- [leetcode] 60. Permutation Sequence 解题报告
- [Leetcode] 60. Permutation Sequence 解题报告
- [leetcode]60. Permutation Sequence@Java解题报告
- Permutation Sequence 求第k个的排列序列 @LeetCode
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
- leetcode 60. Permutation Sequence 第K个全排列
- LeetCode --- 60. Permutation Sequence
- [Leetcode] 60. Permutation Sequence
- [leetcode] 60.Permutation Sequence
- **LeetCode 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- [LeetCode]60. Permutation Sequence
- 数据库中左连接(left join)和右连接(right join)的区别
- mpegts的音视频同步原理
- Hibernate中几种查询的比较_百度文库
- 重写Backbone.js的加载动作
- AngularJS--begining
- leetCode 60.Permutation Sequence (排列序列) 解题思路和方法
- SD卡读写流程
- BC#42第一题
- UML类图几种关系的总结
- 记录下2015申请apple开发者企业账号过程
- 抽象类
- 一些关于Fragment的笔记
- html在套成.aspx页面后 在ie6下变形错位的问题!
- python 使用multiprocessing需要注意的问题