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. POJ 3686 *最小费用流-转化成普通指派问题)

    题意] 有N个订单和M个机器,给出第i个订单在第j个机器完成的时间Mij,每台机器同一时刻只能处理一个订单,机器必须完整地完成一个订单后才能接着完成下一个订单.问N个订单完成时间的平均值最少为多少. ...

  2. java读取配置到Hash表里

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...

  3. Java的JsonHelper

    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <g ...

  4. 数据恢复(Data recovery)

    定义数据恢复: 当存储介质出现损伤或由于人员误操作.操作系统故障本身故障所造成的数据不可见,无法读取.丢失. 工程师通过特殊的手段读取却在正常状态下不可见,不可读,无法读的数据. 数据恢复(Data ...

  5. django 母版与继承

    在子页面的页面最上方使用下面的语法来继承母版. {% extends 'layouts.html' %} 块: 通过在母版中使用{% block xxx %}来定义块.在子页面中通过定义母版中的blo ...

  6. 时间戳date 命令

    时间戳date  命令 [chenxiaohao@iZbp1c0q444r0hp2gq8lhmZ server]$ echo $(date +%Y%m%d)  ——————>今天 2018032 ...

  7. app常见测试点

    安装与卸载: ●应用是否可以在IOS不同系统版本或android不同系统版本上安装(有的系统版本过低,应用不能适配) ●软件安装后是否可以正常运行,安装后的文件夹及文件是否可以写到指定的目录里. ●安 ...

  8. 二叉搜索树-php实现 插入删除查找等操作

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...

  9. pat1088. Rational Arithmetic (20)

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  10. pat1094. The Largest Generation (25)

    1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...