LeetCode- 46/47. Permutations/Permutations || (JAVA) (全排列1,2)
来源:互联网 发布:apache jmeter3.0下载 编辑:程序博客网 时间:2024/06/06 12:57
46. Permutations
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
全排列没有重复数字public class Solution {List<List<Integer>> ret = new ArrayList<>();List<Integer> tmp = new ArrayList<>();public List<List<Integer>> permute(int[] nums) {boolean[] vis = new boolean[nums.length];dfsCore(nums, 0, nums.length);// dfs(nums, 0, nums.length, vis);return ret;}// 使用交换private void dfsCore(int[] nums, int idx, int len) {if (idx == len) {// 找到转置完成后的解,将其存入列表中List<Integer> list = new ArrayList<>();for (int i = 0; i < len; i++) {list.add(nums[i]);}ret.add(list);} // 将当前位置的数跟后面的数交换,并搜索解for (int i = idx; i < len; i++) {swap(nums, idx, i);// 传入idx+1dfsCore(nums, idx + 1, len);swap(nums, idx, i);}}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;}}
public class Solution {List<List<Integer>> ret = new ArrayList<>();List<Integer> tmp = new ArrayList<>();public List<List<Integer>> permute(int[] nums) {boolean[] vis = new boolean[nums.length];dfs(nums, 0, nums.length, vis);return ret;}// DFS+回溯private void dfs(int[] nums, int idx, int len, boolean vis[]) {if (tmp.size() == len) {ret.add(new ArrayList<>(tmp));}for (int i = 0; i < len; i++) {if (vis[i])continue;// 遇到已经加过的元素就跳过vis[i] = true;// 加入该元素后继续搜索tmp.add(nums[i]);// 可以不传i+1参数,但是递归次数增多dfs(nums, i + 1, len, vis);tmp.remove(tmp.size() - 1);vis[i] = false;}}}
47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:
[ [1,1,2], [1,2,1], [2,1,1]]
全排列有重复数字
判断逻辑也可以,跳过相同循环
if (vis[i] || (i > 0 && nums[i - 1] == nums[i]&& !vis[i - 1]))
continue;
public class Solution {List<List<Integer>> ret = new ArrayList<>();List<Integer> tmp = new ArrayList<>();public List<List<Integer>> permuteUnique(int[] nums) {boolean[] vis = new boolean[nums.length];// 排序Arrays.sort(nums);dfs(nums, 0, nums.length, vis);return ret;}// DFS+回溯private void dfs(int[] nums, int idx, int len, boolean vis[]) {if (tmp.size() == len) {ret.add(new ArrayList<>(tmp));}for (int i = 0; i < len; i++) {if (vis[i])continue;// 遇到已经加过的元素就跳过vis[i] = true;// 加入该元素后继续搜索tmp.add(nums[i]);// 可以不传i+1参数,但是递归次数增多dfs(nums, i + 1, len, vis);tmp.remove(tmp.size() - 1);vis[i] = false;///////////////////////////////// 跳过本轮的重复元素,确保每一轮只会加unique的数字while (i < nums.length - 1 && nums[i] == nums[i + 1])i++;}}}
最新测试用例添加了 字典序,此方法不过,
List<List<Integer>> ret = new ArrayList<>();List<Integer> tmp = new ArrayList<>();public List<List<Integer>> permuteUnique(int[] nums) {Arrays.sort(nums);dfsCore(nums, 0, nums.length);return ret;}// 使用交换private void dfsCore(int[] nums, int idx, int len) {if (idx == len) {// 找到转置完成后的解,将其存入列表中List<Integer> list = new ArrayList<>();for (int i = 0; i < len; i++) {list.add(nums[i]);}ret.add(list);} // 将当前位置的数跟后面的数交换,并搜索解for (int i = idx; i < len; i++) {/*if (i > idx && nums[i] == nums[i - 1])continue;*/swap(nums, idx, i);// 传入idx+1dfsCore(nums, idx + 1, len);swap(nums, idx, i);while (i < nums.length - 1 && nums[i] == nums[i + 1])i++;}}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;}
0 0
- LeetCode- 46/47. Permutations/Permutations || (JAVA) (全排列1,2)
- LeetCode-46 Permutations(全排列)
- LeetCode 46 Permutations (全排列)
- leetcode 46,47. Permutations I/II 全排列问题 java
- LeetCode--Permutations 全排列
- Leetcode #47. Permutations II 全排列2 解题报告
- LeetCode | Permutations(全排列)
- leetcode-47. Permutations II(重复元素全排列)
- LeetCode(Permutations) 数列的全排列
- Leetcode:Permutations 数组的全排列
- Leetcode # 46. Permutations 全排列 解题报告
- leetcode之全排列问题(Permutations)
- LeetCode OJ-46.Permutations(全排列问题)
- leetcode 46. Permutations(考全排列)
- 【LeetCode】31. Next Permutations 下一个全排列
- leetcode 46. Permutations 全排列问题+递归
- LeetCode-----46.Permutations&&47.Permutations II (全排列----回溯法)
- 全排列生成 Permutations
- tomcat中处理消息提示的公用类 StringManager.java
- StringTokenizer-大数据情况下截取字符串
- Digester解析XML文件
- 小记,Fragment基础
- RabbitMQ学习之延时队列
- LeetCode- 46/47. Permutations/Permutations || (JAVA) (全排列1,2)
- Linux usermod修改系统账户文件
- 欢迎使用CSDN-markdown编辑器
- 【memcache缓存专题(1)】memcache的介绍与应用场景
- Python总结
- Python错误总结
- echarts 项目使用总结
- nginx配置中proxy_redirect的作用(转)
- leetcode513. Find Bottom Left Tree Value