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):
"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;
}
}
leetCode 60.Permutation Sequence (排列序列) 解题思路和方法的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- [LeetCode] 60. Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- leetcode 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 060 Permutation Sequence 排列序列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,可得到如下序列 (例如, n = 3): 1."123" 2. & ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- R语言学习 - 热图绘制heatmap
生成测试数据 绘图首先需要数据.通过生成一堆的向量,转换为矩阵,得到想要的数据. data <- c(1:6, 6:1, 6:1, 1:6, (6:1)/10, (1:6)/10, (1:6)/ ...
- vue中的input使用e.target.value赋值的问题
很久不写博客了... vue中对表单的处理,相对原生js,增加了一个双向绑定的语法糖:v-model.官方文档里有一段: v-model 会忽略所有表单元素的 value.checked.select ...
- Ubuntu搭建LAMP开发环境
1.安装Apache sudo apt-get install apache2 测试: 浏览器访问 (如:http://localhost),出现It Works!网页. 查看状态: service ...
- PHP 加密:Password Hashing API
PHP 5.5 之后引入 Password hashing API 用于创建和校验哈希密码,它属于内核自带,无需进行任何扩展安装和配置.它主要提供了四个函数以供使用: password_hash(): ...
- Django - 模版之继承
模版继承: 当多个html有共同之处时,需要建立一个模版html(master.html) 1.在模版html中,写入公共的部分: 备注:在模版中,我们可以指定多个block ,其中,可以在标题,cs ...
- [luogu4056 JSOI2009] 火星藏宝图 (贪心 dp)
传送门 Solution 一个显然的贪心:选的点数越多越好.这个随便推推就知道了. 那么我们就贪心的从一列上挑最靠下的转移 直接转移不斜率优化复杂度\(O(nm)\),吸一口O2过了... Code ...
- Go:变量、常量、枚举
一.变量 package main import "fmt" func variableZeroValue() { // 变量声明 var a int var b string f ...
- db2 in out inout
总结: in 给存储过程传的参数,传一个确定的值 out 传的时候传一个非确定的变量v或者“?” .在存储过程里面计算一个结果赋给v,这个变量v在存储过程执行完之后就是一个确定的值了. inou ...
- python3中post请求里带list报错
这个post请求的数据太长,一般data=,json=就够了. 但是今天这个一直报错,用json吧,报缺少参数,用data吧,报多余[. 后来改成data=,并把数据中的[] 用引号括起来," ...
- Jmeter学习笔记之逻辑控制器-Runtime Controller
文章目录 Runtime Controller介绍 Runtime Controller 编辑界面 Once Only Controller介绍 Once Only Controller 配置界面 O ...