新博文地址:[leetcode]PermutationsII
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], and [2,1,1].
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
排列的题应该是很熟悉的,不熟悉的可以看这几道题:Permutations,Next Permutation, 其实,求排列的算法大致一样,这不过这道题要求可以有重复数字,求唯一的排列组合。
说起唯一,自然想起Set,我的算法也完全依赖于set,即,在生成排列序列的时候,先检查一下是否已经存在了,如果存在就不需要返回了。set的key很自然的选择用数组的String表达,遗憾的是,空间复杂度略大......
private String getString(List<Integer> list){ StringBuilder sb = new StringBuilder(); for(int i : list){ sb.append(i); } return sb.toString(); } public List<List<Integer>> permuteUnique(int[] num) { Set<String> set = new HashSet<String>(); List<List<Integer>> result = new ArrayList<List<Integer>>(); if (num == null || num.length == 0) { return result; } if (num.length == 1) { List<Integer> list = new ArrayList<Integer>(); list.add(num[0]); result.add(list); return result; } int[] pre = Arrays.copyOf(num, num.length - 1); List<List<Integer>> prePer = permuteUnique(pre); for (List<Integer> list : prePer) { for (int i = 0; i <= list.size(); i++) { List<Integer> newList = new ArrayList<Integer>(); for(int j = 0; j < i ;j ++){ newList.add(list.get(j)); } newList.add(num[num.length - 1]); for(int j = i; j < list.size(); j++){ newList.add(list.get(j)); } if(!set.contains(getString(newList))){ set.add(getString(newList)); result.add(newList); } } } return result; }
相关推荐
Source file for LeetCode Permutations Problem
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 二.解题思路 主要是有两种...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...
permutations2.py - 返回所有可能的唯一排列 3sum_2.py - 查找数组中所有唯一的三元组,其总和为零。 3sum.py - 查找数组中所有唯一的三元组,其总和为零。 first_last_pos_array.py - 找到给定目标值的开始和结束...
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、LeetCode 题解相关文章,至今收获不少好评。此仓库是公众号内容的补充,包括公众号文章涉及到的题目的参考代码,以及 ...
leetcode 2 LeetCode - 30 Days 前言 相信所有写程式的人在面试前,总是在揣测在白板题会被问到什么问题,而我们最常听到的准备方式就是“刷”。上有数百个可能是面试官问你的题目,我把它...Permutations 目录 ref:
leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_...
permutations import random import itertools import collections from fractions import Fraction from collections import Counter import operator import re from functools import reduce from collections ...
235 Permutations II 571 236 Permutation Sequence 573 237 Generate Parentheses 575 238 Combination Sum 577 239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter ...
II 其他变种 编程之美 Preorder Traversal Inorder Traver sal postorder 非递归 不用栈! 字符串常用操作 字符串 各种转换 Pow(x,n) 优化 Permutations (交换 取子集两种方式) Trie树 11 中序遍历 无堆栈 (前序 ...
2. combinations获取所有的组合情况,permutations可以获取所有的排列情况 3. 限制时间范围 4. 转化为字符型
permutations (normal and better), sum_3.py, unique_paths.py + unique_paths2.py, nQueens.py 很少包含外部库,所以下面的就可以了。 编译任何 C 程序: gcc -o X Xc 执行 python 脚本: chmod 755 X.py --> ....
题目来源:https://leetcode-cn.com/problems/permutations/ 题目 给定一个 没有重复 数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,...
是时候回到Leetcode了2021-03-23 一些常用的内置函数: bin() :信息在collections.Counter :信息在itertools.permutations() :信息在记住,因为它在处理问题时确实可以提供帮助2021-03-24 开始处理HackerRank...
All_Unique_Permutations Best_Time_To_Buy_And_Sell_Stocks_1 Best_Time_To_Buy_And_Sell_Stocks_2 Best_Time_To_Buy_And_Sell_Stocks_3 bst_Iterator 糖果 Character_Precedence Clone_Graph 组合_总和 Counting_...
Permutations II Combinations Letter Combinations of a Phone Number 广度优先搜索 Word Ladder Word Ladder II Surrounded Regions 总结 深度优先搜索 Additive Number Palindrome Partitioning Unique Paths ...