<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 ... 
随机推荐
- NopCommerce 4.2的安装与运行
			一.关于NopCommerce NopCommerce是国外ASP.Net领域一个高质量的B2C开源电商项目,最新版本4.2基于ASP.NET Core MVC 2.2和EF Core 2.2开发,其 ... 
- 21个Java Collections面试问答
			Java Collections框架是Java编程语言的核心API之一. 这是Java面试问题的重要主题之一.在这里,我列出了一些重要的Java集合面试问题和解答,以帮助您进行面试.这直接来自我14年 ... 
- MongoDB自学------(5)MongoDB分片
			分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量. ... 
- laravel中视图的基本使用(七)
			laravel中的视图默认保存在 resources\views 目录下.在控制器中,我们通常使用 view() 方法返回一个视图文件. <?php namespace App\Http\Con ... 
- Windows 10 powershell 中文乱码解决方案
			Windows 10 powershell 中文乱码解决方案 Intro 我装的系统是英文版的 win 10 操作系统,最近使用命令行测试接口,发现中文显示一直异常, 使用网上的各种解决方案都没有效果 ... 
- python执行shell实时输出
			1.使用readline可以实现 import subprocess def run_shell(shell): cmd = subprocess.Popen(shell, stdin=subproc ... 
- numpy 和 tensorflow 中的各种乘法(点乘和矩阵乘)
			点乘和矩阵乘的区别: 1)点乘(即" * ") ---- 各个矩阵对应元素做乘法 若 w 为 m*1 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵. ... 
- 史上最全Winform中使用ZedGraph教程与资源汇总整理(附资源下载)
			场景 C#窗体应用中使用ZedGraph曲线插件绘制图表: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99716066 Win ... 
- LOBs and ORA-01555 troubleshooting (Doc ID 846079.1)
			LOBs and ORA-01555 troubleshooting (Doc ID 846079.1) APPLIES TO: Oracle Database Cloud Schema Servic ... 
- Tomcat下载教程
			首先确定你Windows系统是64位,还是32位(现在大部分是64位) 查看操作系统位数步骤:(WindowsXP,Windows7,Windows8,Windows10查看步骤大同小异,举例Wind ... 
