LeetCode 【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]
]

思路1.这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复后保存结果,但不够理想。

思路2.

对数组先进行排序,如果有重复数字,则跳过,需要注意的是,因为排序,如果采用引用传递,则会破坏排序,所以使用值传递。当然也可以使用引用传递,但每次传递后,把交换的数据交换回来并且对后序数组再进行排序。
class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> result;
	    sort(nums.begin(),nums.end());
	    permuteRec( 0, nums, result);
	    return result;
    }

private:
    void permuteRec( int start, vector<int> nums, vector<vector<int>> &result ){
		if( start >= nums.size()){
			result.push_back(nums);
			return;
		}
		for( int i = start; i < nums.size(); i++ ){
            if( i > start && nums[i] == nums[start]) continue;
            swap( nums[start], nums[i] );
			permuteRec( start+1, nums, result);
		}
    }
};

  

时间: 2024-10-13 11:55:16

LeetCode 【47. Permutations II】的相关文章

leetcode 【 Unique Paths II 】 python 实现

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

leetcode 【 Pascal&#39;s Triangle 】python 实现

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码:oj测试通过 Runtime: 46 ms 1 class Solution: 2 # @return a list of lists of integers 3 def generat

【大话存储II】学习笔记(15章),文件级集群系统

[大话存储II]学习笔记(15章),块级集群存储系统里面分析的主要是块集群系统,同样文件级存储也可以集群化. 因为NAS系统的前端网络是以太网,速度比较低,导致NAS主要用于一些非关键业务中,比如文件共享.但是一些特殊应用也需要多主机同时访问某个大文件,比如3D渲染集群等.如果我们使用块集群存储系统,则会存在一个问题,需要在应用程序上引入文件锁,而NAS的文件系统一般都自带有文件锁机制,所以还不如把NAS存储系统进行集群化. 在谈怎么样把NAS系统进行集群化之前,我们说说集群文件系统的架构.集群

leetCode 47.Permutations II (排列组合II) 解题思路和方法

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

LeetCode 47 Permutations II(全排列)

题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使用递归算法: 传递参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列组合 nums保存初始的数组 used随着计

【Permutations II】cpp

题目: 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]. 代码: class Solution { public: vector<vector<int>>

19.2.7 [LeetCode 47] Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] 1 class Solution { 2 public: 3 vector<vector<int>> permuteUnique(vector<int&