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. Codeforces Round #316 (Div. 2) A

    Description The country of Byalechinsk is running elections involving n candidates. The country cons ...

  2. css兼容写法

    css3 1.box-shadow: filter:progid:DXImageTransform.Microsoft.Shadow(color=#,direction=,strength=);/*兼 ...

  3. Ubuntu上的Python

    在Ubuntu如何查看Python版本 2版本命令:Python -V  (注意是大写) 3版本命令:Python3 -V Ubutun16上默认安装Python 2.7, Python3 将Pyth ...

  4. servlet3

    亿级流量架构 http://www.iteye.com/blogs/subjects/as-core servlet3.1对比 http://jinnianshilongnian.iteye.com/ ...

  5. UML箭头

    继承(泛化):用实线空心三角箭头表示 实现(接口):用虚线空心三角形箭头标示 依赖:虚线箭头,类A指向类B 方法参数需要传入另一个类的对象,就表示依赖这个类 关联:实线箭头,类A指向类B 一个类的全局 ...

  6. hadoop用put上传文件时报错

    用命令-put上传文件 报错0 datanode(s) running 原因是进行了多次格式化 解决办法: 停止集群 删除在hdfs中配置的data目录(即在core-site.xml中配置的hado ...

  7. linux 6 查看防火墙状态及开启关闭命令

    linux 6 查看防火墙状态及开启关闭命令 https://blog.csdn.net/lv_shijun/article/details/52453882 存在以下两种方式: 一.service方 ...

  8. iTween 不能两个游戏对象同时用一个Hashtable

    两个游戏对象,点击其中一个,两个对象一起旋转,再点一下,两个都旋转到原来角度. 如图:两个游戏对象不能用一个Hashtable,会出错,达不到两个一起转的效果. 每个对象要有自己的Hashtable来 ...

  9. adb root错误信息adbd cannot run as root in production builds问题解决

    adb root错误信息adbd cannot run as root in production builds问题解决 一.问题描述 1.输入指令 >adb root adbd cannot ...

  10. (转)Python中的split()函数的用法

    Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...