<BackTracking> permutation 254 47 60
254. Factor Combinations
class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<>();
//corner case
if(n < 4) return res;
dfs(n, 2, res, new ArrayList<Integer>());
return res;
}
private void dfs(int n, int index, List<List<Integer>> res, List<Integer> subset){
if(n == 1){
if(subset.size() > 1){
res.add(new ArrayList<Integer>(subset));
}
return;
}
//拆解
for(int i = index; i <= n; i++){
if(i > n / i) break;
if(n % i == 0){
subset.add(i);
dfs(n / i, i, res, subset);
subset.remove(subset.size() - 1);
}
}
subset.add(n);
dfs(1, n, res, subset);
subset.remove(subset.size() - 1);
}
}
47. Permutations II
有重复元素: gren tea
nums[i] == nums[i - 1] && !visited[i - 1] continue. 两数相等直接跳过。 !visited[i - 1] 如果前面的数字没选取过,则不能选后面这个相同的数字
//排列(雨露均沾)
//nums[i] == nums[i - 1] && !visited[i - 1] continue. 两数相等直接跳过
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
//corner case
if(nums == null || nums.length == 0) return res;
Arrays.sort(nums);
boolean[] visited = new boolean[nums.length];
for(int i = 0; i < nums.length; i++){
visited[i] = false;
}
dfs(nums, res, new ArrayList<Integer>(), visited);
return res;
} private void dfs(int[] nums, List<List<Integer>> res, List<Integer> subset, boolean[] visited){
if(subset.size() == nums.length){
res.add(new ArrayList<Integer>(subset));
return;
}
for(int i = 0; i < nums.length; i++){
if(visited[i]) continue;
if(i >= 1 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
subset.add(nums[i]);
visited[i] = true;
dfs(nums, res, subset, visited);
subset.remove(subset.size() - 1);
visited[i] = false;
} }
}
60. Permutation Sequence
例: n = 4, k = 14。
k 转化为 第k个数的下标,则k--。 eg: k = 13(只需做一次)
1.每次第一顺位的数值 :at = k / (n - 1)!。 at = 13 / 3! = 2。
第二顺位数字的位置: k = k % (n - 1)!。 k = 13 % 3! = 1。 ans = 3。 对于以3为开头的6个数字中,第二顺位数字排在第2位。
2. at = k / (n - 1)!。 at = 1 / 2! = 0。 k''的排第2,剩下数字的周期是 2!。
k = k % (n - 1)。 k = 1 % 2! = 1。 ans = 31。
3. at = k / 1!。 at = 1 / 1 = 1。
k = k % 1!。 k = 1 % 1 = 0。 ans = 314。
4。 at = k / 1。 at = 0。 ans =3142。
class Solution {
public String getPermutation(int n, int k) {
List<String> table = new LinkedList<>(map(n));
if(n == 1) return table.get(0);
k--;
String ans = "";
while(n > 0){
int kth = kth(n--);
int at = k / kth;
k %= kth;
ans += table.get(at);
table.remove(at);
}
return ans;
}
public List<String> map(int n){
String s = "1,2,3,4,5,6,7,8,9";
List<String> list = Arrays.asList(s.split(","));
return list;
}
public int kth(int n){
int t = 1;
for(int i = 1; i < n; i++){
t *= i;
}
return t;
}
}
<BackTracking> permutation 254 47 60的更多相关文章
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- Pandas的高级操作
pandas数据处理 1. 删除重复元素 使用duplicated()函数检测重复的行,返回元素为布尔类型的Series对象,每个元素对应一行,如果该行不是第一次出现,则元素为True keep参数: ...
- 获取文本的编码类型(from logparse)
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...
- PROJ4初探(转并整理格式)
PROJ4初探(转并整理格式) Proj4是一个免费的GIS工具,软件还称不上. 它专注于地图投影的表达,以及转换.采用一种非常简单明了的投影表达--PROJ4,比其它的投影定义简单,但很明显.很容易 ...
- ZAM 3D 制作简单的3D字幕 流程(二)
原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...
- Google的IP地址一览表,加上代理服务器
Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93.123.23.4 93.123.23.5 93.123.23.6 93.123.23.7 93.123. ...
- google(转帖)
本帖最后由 qiushui_007 于 2014-6-10 16:14 编辑 IP Addresses of Google Global Cachewww.kookle.co.nr Bulgaria ...
随机推荐
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
- golang数据结构之递归解决迷宫问题
简单来说:递归就是函数/方法自己调用自己,只是每次传入不同的变量. 递归可以解决各种数学问题:n皇后问题.阶乘问题.汉诺塔.迷宫问题.球和篮子问题等等: maze.go package maze im ...
- mac--“-bash: brew: command not found”,怎么解决?
报错 “-bash: brew: command not found” 执行下面命令,安装HomeBrew ruby -e "$(curl -fsSL https://raw.githubu ...
- web开发中移动端适配
这个话题有些复杂,说起来有些琐碎,因为和移动端适配相关的问题太多了. 1. 概念 1.1 设备像素 设备像素被称为物理像素,它是显示设备中一个最小的物理部件.每个像素可以根据操作系统设置自己的颜色和亮 ...
- oracle学习笔记(十三) 查询练习(三) 子查询查询
子查询练习 create table empployee_demo( empno number(4) not null primary key, --员工编号,主键 ename varchar2(10 ...
- Docker开启Remote API 访问 2375端口
Docker常见端口 我看到的常见docker端口包括: 2375:未加密的docker socket,远程root无密码访问主机2376:tls加密套接字,很可能这是您的CI服务器4243端口作为h ...
- 接口测试基础--Fiddler、Postman、Python实现对接口的测试
写在前面:本文主要的章节规划: 1.什么是接口测试 另外,有的时候会直接调用别的公司的接口,比如银行的.淘宝的.支付宝的,此时也需要做接口测试以及验证数据: 做接口测试的好处: 其中, ...
- Zuul 1.x 的工作原理
Zuul简介 Zuul在微服务架构中,可以作为提供动态路由,监控,弹性,安全等边缘服务的框架.在Netflix,被用作所有请求到达streaming application的前门.Zuul使用一系列不 ...
- C# Stocket
介绍 1.TCP/IP(Transmission Control Protocol/Internet Protocol) 即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域网(WANs)设计 ...
- npm ERR! code Z_BUF_ERROR
最新学习egg,在npm install egg --save 步骤中总是报错如下: npm ERR! code Z_BUF_ERROR npm ERR! errno -5 npm ERR! zlib ...