leetcode:回溯——permutation-sequence,
1. permutation-sequence 顺序排列第k个序列
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 k th permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
public class Solution {
public String getPermutation(int n, int k) {
// initialize all numbers
ArrayList<Integer> numberList = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
numberList.add(i);
}
// change k to be index
k--;
// set factorial of n
int mod = 1;
for (int i = 1; i <= n; i++) {
mod = mod * i;
}
String result = "";
// find sequence
for (int i = 0; i < n; i++) {
mod = mod / (n - i);
// find the right number(curIndex) of
int curIndex = k / mod;
// update k
k = k % mod;
// get number according to curIndex
result += numberList.get(curIndex);
// remove from list
numberList.remove(curIndex);
}
return result.toString();
}
}
leetcode:回溯——permutation-sequence,的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- Java for LeetCode 060 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 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 the p ...
- 【leetcode】 Permutation Sequence (middle)
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 ...
- leetcode 之 Permutation Sequence
Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【Leetcode】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 the p ...
- 【leetcode】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
随机推荐
- Linux字符设备驱动--Led设备驱动
①驱动源码 #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #i ...
- 修改jupyter notebook的默认浏览器
1.打开命令行 2.输入jupyter notebook --generate-config 3.显示出jupyter_notebook_config.py 文件所在的目录.按文件目录找到这个文件. ...
- my01_Mysql router 安装
Mysql router 主要用途是读写分离,主主故障自动切换,负载均衡,连接池等.安装如下 下载地址:https://dev.mysql.com/downloads/router/ tar -zxv ...
- 信息领域热词分析系统--java爬取CSDN中文章标题即链接
package zuoye1; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLExce ...
- java——volatile的可见性不能保证线程安全
volatile: 1.保证变量对所有线程的可见性(但是由于java里面的运算并非原子操作,导致volatile变量的运算在并发下一样是不安全的) 用代码试过,确实是这样的,原因:有可能同时多个thr ...
- Python import搜索路径相关
import搜索路径 在当前目录下搜索该模块 在环境变量 PYTHONPATH 中指定的路径列表中依次搜索 在 Python 安装路径的 lib 库中搜索 查看当前的搜索路径 import sys p ...
- silverlight vs2010 需要缺少的web组件才能加载
在打开一个开源的Silverlight项目是遇到如图所示的问题,点击是后没有反应. 查了资料,需要安装微软的webpi(Microsoft Web Platform Installer) webpi的 ...
- Linux 文件锁flock 实现两个进程相互监听存活状态
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指 ...
- Murano Weekly Meeting 2015.10.20
Meeting time: 2015.October.20th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting sum ...
- (转) IP子网划分
原文:http://blog.csdn.net/birdie_l/article/details/77994610 子网划分公式计算法 实例一 实例二 心算思路总结: B类公式算法举例: 总结:此表 ...