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. oracle Clob类型转换成String类型

    转载:https://www.cnblogs.com/itmyhome/p/4131339.html Clob类型转换成String类型 oracle中表结构如下: create table GRID ...

  2. 傻瓜式Spring教学第二课

    什么是依赖注入 先说什么是依赖 如下: class A{ B b; } class B{ } 则称A依赖B. 依赖:A的某些业务逻辑需要B的参与,如果不对A中的参数b进行实例化,那么A中的某些业务逻辑 ...

  3. 惠普台式机在UEFI BIOS设置通电自动开机 影响电脑自动重启关不了机设置

    设置通电自动开机 影响电脑自动重启关不了机设置   惠普台式机在UEFI BIOS中 1. 开机时不断点击F10键进入BIOS,选择Advanced(高级)然后选择Boot Options,点击回车 ...

  4. vue-踩过的坑

    1)引入组件时路径一定要是./ or ../开头 import Goback from './public/goback.vue' 2)这类输入框绑定的值不是:value 不是 :value 不然数据 ...

  5. js学习笔记 -- await/ async

    await 暂停async function函数,等待Promise处理完成,若Promise 状态为fulfilled,其回调resolve的参数作为await的值,Promise 状态为rejec ...

  6. centeros 6.5 网络设置

    vi  /etc/sysconfig/network-script/ifcfg-eth0 关键点  nat设置中的网关,dhcp设置中起始网络地址跟结束ip地址,选择其中范围的一个即可 service ...

  7. 前后端分离之JWT用户认证

    在前后端分离开发时为什么需要用户认证呢?原因是由于HTTP协定是不储存状态的(stateless),这意味着当我们透过帐号密码验证一个使用者时,当下一个request请求时它就把刚刚的资料忘了.于是我 ...

  8. equals()方法 与 == 区别

    equals()方法 与 == 区别 : equals():在字符串中比较内容,则必须使用equals(变量),如果相等则为true,否则为false == :使用==比较的是地址是否一致 class ...

  9. python 读取文件使用chunksize后逐块迭代操作

    chunkers=pd.read_csv('dd.csv',chunksize=10000) tot=pd.Series([]) for piece in chunkers: tot=tot.add( ...

  10. VS Ctrl + Shift + Q

    在VS中按 Ctrl + Shift + Q 可以快速查找 void OnCollisionStay  等方法. void OnCollisionStay(Collision collision) { ...