LeetCode-全排列
来源:互联网 发布:php开源电子商务系统 编辑:程序博客网 时间:2024/05/29 09:51
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]]
求全排列,DFS的典型应用
package solutions._46;/** * 46. Permutations */import java.util.ArrayList;import java.util.LinkedList;import java.util.List;class Solution { private boolean visited[]; private List<List<Integer>> result; private List<Integer> cur; private void DFS(int[] nums, int len) { if (len == nums.length) { result.add(new ArrayList<>(cur)); return; } for (int i = 0; i < nums.length; i++) { if (!visited[i]) { visited[i] = true; cur.add(nums[i]); DFS(nums, len + 1); visited[i] = false; cur.remove(cur.size() - 1); } } } public List<List<Integer>> permute(int[] nums) { visited = new boolean[nums.length]; result = new LinkedList<>(); cur = new LinkedList<>(); DFS(nums, 0); return result; } public static void main(String[] args) { Solution solution = new Solution(); int[] arr = {1, 2}; List<List<Integer>> lists = solution.permute(arr); System.out.println(lists); }}
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
基本类似于全排列,只不过有顺序性从而避免重复([1,4]和[4,1]一样)。
package solutions._77;/** * 77. Combinations */import java.util.ArrayList;import java.util.LinkedList;import java.util.List;class Solution { private boolean[] visited; private List<List<Integer>> result; private List<Integer> cur; private int n; private int k; private void DFS(int pos, int len) { if (len == k) { result.add(new ArrayList<>(cur)); return; } for (int i = pos; i <= n; i++) { if (!visited[i]) { visited[i] = true; cur.add(i); DFS(i, len + 1); cur.remove(cur.size() - 1); visited[i] = false; } } } public List<List<Integer>> combine(int n, int k) { visited = new boolean[n + 1]; cur = new LinkedList<>(); result = new LinkedList<>(); this.n = n; this.k = k; DFS(1, 0); return result; } public static void main(String[] args) { Solution solution = new Solution(); List<List<Integer>> lists = solution.combine(4, 2); System.out.println(lists); }}
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]]
可以将结果集保存在set中,有点投机取巧了。
package solutions._47;/** * 47. Permutations II */import java.util.*;class Solution { private boolean visited[]; private Set<List<Integer>> sets; private List<Integer> cur; private void DFS(int[] nums, int len) { if (len == nums.length) { sets.add(new ArrayList<>(cur)); return; } for (int i = 0; i < nums.length; i++) { if (!visited[i]) { visited[i] = true; cur.add(nums[i]); DFS(nums, len + 1); visited[i] = false; cur.remove(cur.size() - 1); } } } public List<List<Integer>> permuteUnique(int[] nums) { visited = new boolean[nums.length]; sets = new HashSet<>(); cur = new LinkedList<>(); DFS(nums, 0); List<List<Integer>> result = new LinkedList<>(); result.addAll(sets); return result; } public static void main(String[] args) { Solution solution = new Solution(); int[] arr = {1, 1, 0, 0, 1, -1, -1, 1}; List<List<Integer>> lists = solution.permuteUnique(arr); System.out.println(lists); }}
阅读全文
0 0
- leetcode 全排列系列
- Leetcode全排列问题
- 关于全排列 leetcode
- LeetCode--Permutations 全排列
- LeetCode-全排列
- [LeetCode]Permutation全排列和去重全排列
- LeetCode | Permutations(全排列)
- LeetCode-46 Permutations(全排列)
- leetcode之全排列(Permutation)
- LeetCode 46 Permutations (全排列)
- leetcode-全排列(permutation)
- LeetCode(Permutations) 数列的全排列
- Leetcode:Permutations 数组的全排列
- Leetcode全排列问题Java版
- Leetcode # 46. Permutations 全排列 解题报告
- leetcode之全排列问题(Permutations)
- LeetCode OJ-46.Permutations(全排列问题)
- leetcode 46. Permutations(考全排列)
- CCF—消除类游戏 20151202
- Java中的static关键字解析
- sizeof 知多少?
- 商品库存信息管理
- HDU 3401 Trade(用单调队列优化DP)
- LeetCode-全排列
- 51nod 1005大数相加
- 实验一线性表的基本操作实现及其应用
- A Cubic number and A Cubic Number
- 2017多校训练赛第四场 HDU 6078 Wavel Sequence(dp+优化)
- 算法竞赛入门经典(第2版)笔记--第3章
- jdbc创建后创建bean
- linux中编译安装Apache、PHP、MySQL(上)
- MySQL数据库-错误1166 Incorrect column name