Permutation

class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
int visited[];
public List<List<Integer>> permute(int[] nums) {
visited = new int[nums.length];
//foreach pos, get tall number
ArrayList<Integer> pos = new ArrayList<>();
back(pos,nums, visited);
return res;
}
void back(ArrayList<Integer> pos, int[] nums, int[] visited){
if(pos.size()>=nums.length){
List<Integer> temp =new ArrayList<>(pos);//!!!!!!!!!!!why copy this, immunatable like string (always deal with only one list)
res.add(temp);
return;
}
for(int i = 0; i<nums.length; i++){
if(visited[i]==0){
pos.add(nums[i]);
visited[i] = 1;//index of nums
back(pos, nums,visited);
visited[i] = 0;
pos.remove(pos.size()-1);
}
}
}
}

the structure of backtracking

why copy the list

generic list

47: duplicate elements

if contains the element

class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
int visited[];
public List<List<Integer>> permuteUnique(int[] nums) {
visited = new int[nums.length];
//foreach pos, get tall number
ArrayList<Integer> pos = new ArrayList<>();
back(pos,nums, visited);
return res;
}
void back(ArrayList<Integer> pos, int[] nums, int[] visited){
if(pos.size()>=nums.length){
List<Integer> temp =new ArrayList<>(pos);//!!!!!!!!!!!why copy this, immunatable like string (always deal with only one list)
if(!res.contains(temp))
res.add(temp);
return;
}
for(int i = 0; i<nums.length; i++){
if(visited[i]==0){
pos.add(nums[i]);
visited[i] = 1;//index of nums
back(pos, nums,visited);
visited[i] = 0;
pos.remove(pos.size()-1);
}
} }
}

why both using visited array

Because we can only use each element once

Combination accoring order

class Solution {
//for each position(two) set number
List<List<Integer>> res = new ArrayList<List<Integer>>();
boolean[] visited ;
public List<List<Integer>> combine(int n, int k) {
visited = new boolean[n+1];
back(0, n,k, new ArrayList<Integer>(),1);
return res;
}
void back(int pos, int n, int k, List<Integer> list, int num){
if(pos>=k){
List<Integer> temp = new ArrayList(list);
res.add(temp);
return;
}
for(int i = num; i<=n; i++){
if(!visited[i]){
list.add(i);
visited[i] = true;
back(pos+1, n, k, list,i+1);
visited[i] = false;
list.remove(list.size()-1);
}
}
}
}

//

check the num in back function

we need increasing number because (1,2) and (2,1) are the same things

//prev: 137 317 (without order)
//contains 1 2 3 , 1 2 3,
//visited 1 1 5, 1 5 1, visite element once for each path

Leetcode 46 47 Permutation, 77 combination的更多相关文章

  1. LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)

    LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...

  2. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  4. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  5. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  6. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  7. leetcode 46. 全排列 及 47. 全排列 II

    46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...

  8. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  9. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

随机推荐

  1. day_11 生成器

    . 生成器本身是迭代器 .写迭代器的方式 .生成器函数 .生成器表达式 .各种推导式 .生成器函数 函数内部有yield, yield返回->return yield把函数分段进行 生成器可以使 ...

  2. 一个基于QT简单登录对话框(带验证码功能)

    1. 对话框样式 2. 源代码 ①. main.cpp #include <QtGui/QApplication> #include "QLoginDialog.h" ...

  3. case when null then 'xx' else 'yy' end 无效

    Sql Server 中使用case when then 判断某字段是否为null,和判断是否为字符或数字时的写法不一样,而且语法能正常执行, 如果不注意数据内容,很容易搞错. 错误方法: CASE ...

  4. spring boot+jaspersoft实现复杂报表

    1.实现效果: 2.下载 jaspersoft分为社区版和商业版,以下网址是社区版:https://community.jaspersoft.com/community-download

  5. Dev Express Report 学习总结(八)Dev Express Reports 常见问题总结

    1. 在新建ASP.NET Dev Express Report时的两种方式: A. 右键Add DevExpress Item->New Item->All->从Web Repor ...

  6. tomcat正常关闭,端口号占用解决 StandardServer.await: create[8005]:

    cmd进入依次输入以下三个命令 1:netstat -ano | findstr "8005"2:tasklist | findstr "5632" 3:tas ...

  7. 11073 最热门的K个搜索串

    11073 最热门的K个搜索串时间限制:350MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VCDescription大家都非常喜欢而习惯用baidu ...

  8. 微服务(Micro Service Architecture)浅析

    最近一段时间,微服务的概念很火,可能是跟Docker技术的快速发展和壮大有一定的关系,同时借助于Uber.Netflix.Groupon等公司的实践.宣传和推广,使得MSA渐渐地成为企业或者架构师讨论 ...

  9. Qt 学习

    Qt 学习 C++ 模版 QObject 提供一个十分有用的 api,T findChild(QString, Qt::FindChildOptions),这个函数接收一个模版参数,返回模版参数的类型 ...

  10. 如何解读IL代码

    如何解读IL代码 关于IL代码,我有将从三个方面去揭开它神秘的面纱.IL代码是什么?我们为什么要去读懂IL代码?我们如何去读懂IL代码?这三个问题的解答,将是我解读IL代码的整体思路. IL代码是什么 ...