(Leetcode)46&47 Permutations--LinkedList and HashSet
来源:互联网 发布:软件系统需求分析报告 编辑:程序博客网 时间:2024/04/30 09:53
Problem
46.Permutations
Given a collection of distinct numbers, return all possible permutations.
47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Analysis
都是求一个数组的全排列。
46 没有重复数字,47有重复数字。
Solution
先求2个数的全排列,把第三个数插入上面的排列,知道nums[]所有的数都取完。
使用了LinkedList
47中为了去除相同的排列,用Hashset来做了一个标记,保证每个排列不重复。
Code
46
package _046Permutations;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class Solution { public List<List<Integer>> permute(int[] nums) { LinkedList<List<Integer>> permutations = new LinkedList<List<Integer>>(); if(nums==null) return null; LinkedList<Integer> firstnum = new LinkedList<Integer>(); firstnum.add(nums[0]); permutations.add(firstnum); for(int i=1; i<nums.length; i++){ while(permutations.peek().size()==i){ List<Integer> subPermutations = permutations.remove(); for(int j=0; j<=subPermutations.size(); j++){ List<Integer> new_subPermutations = new LinkedList<Integer>(subPermutations); new_subPermutations.add(j, nums[i]); permutations.add(new_subPermutations); } } } return permutations; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int nums[] = {1,2,3}; List<List<Integer>> testList = new Solution().permute(nums); for (int i = 0; i < testList.size(); i++) { for (int j = 0; j < testList.get(i).size(); j++) { System.out.print(testList.get(i).get(j)+" "); } System.out.println(); } }}
47
package _47PermutationsII;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Set;public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { Arrays.sort(nums); LinkedList<List<Integer>> permutations = new LinkedList<List<Integer>>(); if(nums==null) return null; Set<String> markBoard = new HashSet<String>(); LinkedList<Integer> firstnum = new LinkedList<Integer>(); firstnum.add(nums[0]); permutations.add(firstnum); for(int i=1; i<nums.length; i++){ while(permutations.peek().size()==i){ List<Integer> subPermutations = permutations.remove(); markBoard.remove(subPermutations.toString()); for(int j=0; j<=subPermutations.size(); j++){ List<Integer> new_subPermutations = new LinkedList<Integer>(subPermutations); new_subPermutations.add(j, nums[i]); if(markBoard.add(new_subPermutations.toString())) permutations.add(new_subPermutations); } } } return permutations; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int nums[] = {1,2,2,1}; List<List<Integer>> testList = new Solution().permuteUnique(nums); for (int i = 0; i < testList.size(); i++) { for (int j = 0; j < testList.get(i).size(); j++) { System.out.print(testList.get(i).get(j)+" "); } System.out.println(); } System.out.println(testList.size()); }}
0 0
- (Leetcode)46&47 Permutations--LinkedList and HashSet
- leetcode 46-Permutations and 47-Permutations II
- LeetCode 46 Permutations + LeetCode 47 Permutations II
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode 46/47 Permutations, Permutations II
- [Leetcode]Permutations and Permuations II
- LinkedList HashSet
- [LeetCode 46 & 47]Permutations I & II
- <LeetCode OJ> 47/46 Permutations (II / I)
- [Leetcode] #46#47 Permutations I & II
- [leetcode 47] Permutations II
- leetcode || 47、 Permutations II
- leetcode 47 : Permutations II
- leetcode 47: Permutations II
- leetcode 47:Permutations II
- 【leetcode】【47】Permutations II
- LeetCode 47 - Permutations II
- linux load averages
- __weak typeof(self) _self = self;
- memcached基本配置与使用
- C++:删除map元素,当value值为指针时,如何释放内存。
- Emmet for Dreamweaver:HTML/CSS代码快速编写神器
- (Leetcode)46&47 Permutations--LinkedList and HashSet
- android app图标未读消息数(BadgeNumber)
- Python学习笔记 —— mysql数据库使用
- 【spring】BeanFactoryPostProcessor使用场景之PropertyPlaceholderConfigurer
- 领土划分
- 欢迎使用CSDN-markdown编辑器
- Android布局:include、merge 、ViewStub标签区别
- Android Vector曲折的兼容之路
- HP-UX创建裸设备