Leetcode 46 47 Permutation, 77 combination
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的更多相关文章
- 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 ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- leetcode 46. 全排列 及 47. 全排列 II
46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
随机推荐
- java的Spring学习2--构造函数注入
bean文件如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi=" ...
- hdu3746 KMP-next数组的应用
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- sqlserver 事务日志
预写式日志(Write-Ahead Logging (WAL)) --在数据写入到数据库之前,先写入到日志. 1.”Begin Tran”记录 -> 缓冲区 2. 日志 ...
- python--upload file into HDFS 加载文件到HDFS
模拟:https://creativedata.atlassian.net/wiki/spaces/SAP/pages/61177860/Python+-+Read+Write+files+from+ ...
- 前后端分离之JWT用户认证
在前后端分离开发时为什么需要用户认证呢?原因是由于HTTP协定是不储存状态的(stateless),这意味着当我们透过帐号密码验证一个使用者时,当下一个request请求时它就把刚刚的资料忘了.于是我 ...
- 五种I/O模型的学习
来自 http://www.52im.net/thread-1935-1-1.html 4.互联网服务端处理网络请求的原理 首先看看一个典型互联网服务端处理网络请求的典型过程:<ignore ...
- 吞吐率(Requests per second),缩写RPS
计算公式: 吞吐率 = 总请求数 / 处理这些请求的总完成时间 Requests per second = Complete requests / Time taken for tests 吞 ...
- 《我在谷歌大脑见习机器学习的一年:Node.js创始人的尝试笔记》阅读笔记
文章来源:https://www.toutiao.com/i6539751003690893828/?tt_from=weixin_moments&utm_campaign=client_sh ...
- inventor安装失败怎样卸载安装inventor 2014?
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- Java基础12-数组求最大值以及排序
1.求最大值 实例:小明买手机,询问了4家店的价格,分别是2800,2900,2600,3000,输出最低价 正确代码:类似打擂台,将数组第一个值赋给最小值,然后依次比较 import java.ut ...