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);
}
}
};
LeetCode 【47. Permutations II】的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- LeetCode OJ 47. Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
随机推荐
- Scrum Meeting 2-20151202
任务安排 姓名 今日任务 明日任务 困难 董元财 完成下拉刷新的实现 请假(明天是编译截至最后一天) 无 胡亚坤 完成圆形头像代码设计 请假(明天是编译截至最后一天) 无 刘猛 学习listview的 ...
- SPSS数据分析—重复测量差分析
多因素方差分析中,每个被试者仅接受一种实验处理,通过随机分配的方式抵消个体间差异所带来的误差,但是这种误差并没有被排除.而重复测量设计则是让每个被试接受所有的实验处理,这样我们就可以分离出个体差异所带 ...
- Windows Store App 获取文件及文件夹列表
通过使用13.2.1小节给出的方法和属性,不仅可以对用户库中的文件和文件夹进行操作,还可以获取其中所有的文件或者文件夹,比如为了完整地展现整个音乐库,可以获取并列举出音乐库中所有的音乐文件,以便能够在 ...
- Swift 02.Array
数组可以存放任意类型,初始化时候的类型 决定了数组后面可以添加什么类型的元素 let 不可变数组 let arrayC = [,,,,,,] var 可变数组 var arrayM = [,,,,,, ...
- sprite图在移动端的使用
做移动端页面时,设计稿中的切片图片往往是实际的2倍,此处记录图片正常显示大小的技巧. 当图片是单张的话,可以对容器设计实际大小,然后设置background-image,为了让图片缩小一倍,可以设置b ...
- Android - 定时服务 - Timer
注:在项目中,有时可能会有一些定时执行的任务,这时,一般都会在一个service中写一个定时器. 例: Service类: import java.util.Timer; import java.ut ...
- jsp里边下载文件
//Tomcat下需要这两个//out.clear();//out=pageContext.pushBody(); //weblogic下加上会报错 response.reset();// 清空输出流 ...
- Android调用相册拍照控件实现系统控件缩放切割图片
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家 package cn.m15.test; import java.io.By ...
- Asp.net MVC 视图(四)
强类型辅助方法 模板辅助方法 Asp.net MVC中的模板辅助方法利用元数据和模板构建HTML,即:模板辅助方法可以通过使用数据注解,在运行时使用合适的任何“编辑器”来生成合适的HTML标记元数据包 ...
- 基于httpClient的https的无秘钥登陆
HttpClient package com.mediaforce.crawl.util; import java.util.ArrayList; import java.util.HashMap; ...