- 浏览: 183537 次
- 性别:
- 来自: 济南
文章分类
最新评论
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].
与Permutations 这道题不同的是数组中包含重复的元素,有重复的元素我们就不能通过值来判断当前元素是否已经被记录,我们可以通过下标来记录元素是否被记录,因为每个元素的下标是唯一的,这是我们用一个辅助数组isVisited来记录,并且isVisited要同结果集一同回溯。此外数组中可能包含很多重复元素,因此我们可以先将数组排序,然后在回溯时,记录删除的元素,当回溯后继续往前走时,首先判断当前元素与之前删除的元素是否相同,如果相同就直接跳过。代码如下:
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
与Permutations 这道题不同的是数组中包含重复的元素,有重复的元素我们就不能通过值来判断当前元素是否已经被记录,我们可以通过下标来记录元素是否被记录,因为每个元素的下标是唯一的,这是我们用一个辅助数组isVisited来记录,并且isVisited要同结果集一同回溯。此外数组中可能包含很多重复元素,因此我们可以先将数组排序,然后在回溯时,记录删除的元素,当回溯后继续往前走时,首先判断当前元素与之前删除的元素是否相同,如果相同就直接跳过。代码如下:
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { ArrayList<Integer> list = new ArrayList<Integer>(); ArrayList<List<Integer>> llist = new ArrayList<List<Integer>>(); ArrayList<Integer> isVisited = new ArrayList<Integer>(); if(nums == null || nums.length == 0) return llist; java.util.Arrays.sort(nums); getPermute(0, nums, isVisited, list, llist); return llist; } public void getPermute(int start, int[] nums, ArrayList<Integer> isVisited, ArrayList<Integer> list, ArrayList<List<Integer>> llist) { if(list.size() == nums.length) { if(!llist.contains(list)) llist.add(new ArrayList<Integer>(list)); } int former = Integer.MIN_VALUE; for(int i = start; i < nums.length; i++) { if(former == nums[i]) continue; if(!isVisited.contains(i)) { list.add(nums[i]); isVisited.add(i); getPermute(0, nums, isVisited, list, llist); former = list.remove(list.size() - 1); isVisited.remove(isVisited.size() - 1); } } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
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 ...
PermutationsII permutations = new PermutationsII(); int[] nums = {1, 1, 2}; List<List<Integer>> result = permutations.permuteUnique(nums); for (List<Integer> permutation : result) { System.out....
js js_leetcode题解之47-permutations-ii.js
c语言入门 C语言_leetcode题解之47-permutations-ii.c
第47题"全排列II"(Permutations II)是LeetCode中的一道经典题目,它涉及到组合数学和递归的思想。在本题中,任务是找出所有可能的无重复字符的全排列。 全排列问题通常使用回溯法来解决,这是一种通过尝试所有...
### Fourier Theoretic Probabilistic Inference over Permutations #### Introduction This paper discusses a unique method for probabilistic inference over permutations, which are fundamental in a wide ...
Richard P. Stanley的经典文章A Survey of Alternating Permutations,供有需要者下载
- Permutations / Permutations II: 生成所有可能的排列组合,考虑重复元素的情况。 - Rotate Image: 给定一个二维矩阵,将它顺时针旋转90度。 - Group Anagrams: 组合所有字母异位词,即将字母顺序不同但字母组合...
Permutations Medium 回溯 0047 Permutations II Medium 递归、回溯 0051 N-Queens Hard 回溯 0053 Maximum Subarray Easy 动态规划 0069 Sqrt(x) Easy 二分、牛顿迭代 0070 Climbing Stairs Easy 动态规划 0075 ...
在编程领域,特别是使用Python语言时,"permutations"是一个重要的概念,它涉及到组合学和算法设计。排列是指从给定的元素集中选取若干个元素,并按照一定的顺序进行排列的所有可能方式。在Python中,我们可以使用...
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、LeetCode 题解相关文章,至今收获不少好评。...Permutations全排列 Permutations II全排列 II Maximum Suba
046:Permutations 047:Permutations II 051:N-Queens 052:N-Queens II 071: Letter Combinations of a Phone Number 093:Restore IP Addresses 树的遍历问题也可以用这种思想来解释。只不过是特殊的递归而已。...
安装$ npm install --save permutations-count 用法 var permutationsCount = require ( 'permutations-count' ) ;var numPermutations = permutationsCount ( 12 , 3 ) ;console . log ( numPermutations ) ; // =>...
js js_leetcode题解之46-permutations.js
c语言入门 C语言_leetcode题解之46-permutations.c
【信息安全与数据安全:Lossy Trapdoor Permutations的深度探讨】 在信息安全领域,数据安全是至关重要的一环。Lossy Trapdoor Permutations(LTP)是一种用于保护数据安全的加密技术,它结合了trapdoor函数和lossy...
数字排列重复数排列Расчётколичествастроквфайлепроизвёлкомандой:.. \ permutations.txt“ |测量对象-线ВWindouse 标题:行字字符属性30240