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], and [2,1,1].
分析
用上一题的代码,完全可以AC,那是因为我们的库函数next_permutation()以及prev_permutation()内置排重的代码。。。
其实,题目考察的实质,是让我们自己实现全排,只不过我偷懒了,直接调用了算法库。。。
AC代码
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int> > ret;
if (nums.empty())
return ret;
sort(nums.begin(), nums.end());
ret.push_back(nums);
while (next_permutation(nums.begin(), nums.end()))
ret.push_back(nums);
return ret;
}
};
LeetCode(47)Permutations II的更多相关文章
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(47):全排列 II
Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...
- LeetCode(52) N-Queens II
题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...
- LeetCode(46)Permutations
题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...
- Leetcode(213)-打家劫舍II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(90) Subsets II
题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
随机推荐
- 【转】Hive安装及使用攻略
Posted: Jul 16, 2013 Tags: HadoophiveHiveQLsql分区表 Comments: 18 Comments Hive安装及使用攻略 让Hadoop跑在云端系列文章, ...
- nginx 配置tp3.2
server { listen 80; server_name 域名; #charset koi8-r; #access_log /var/log/nginx/host.access.log main ...
- SQL 初级教程学习(四)
1. union,union all SELECT E_Name FROM Employees_ChinaUNIONSELECT E_Name FROM Employees_USA 默认地,UNION ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- 1.1.2最小生成树(Kruskal和Prim算法)
部分内容摘自 勿在浮沙筑高台 http://blog.csdn.net/luoshixian099/article/details/51908175 关于图的几个概念定义: 连通图:在无向图中,若任意 ...
- 构造 BestCoder Round #52 (div.2) 1001 Victor and Machine
题目传送门 题意:有中文版的 分析:首先要知道机器关闭后,w是清零的.所以一次(x + y)的循环弹出的小球个数是固定的,为x / w + 1,那么在边界时讨论一下就行了 收获:这种题目不难,理解清楚 ...
- JAVA中abstract,interface,final,static语法
转自:http://www.cnblogs.com/yueue/archive/2010/04/20/1715863.html 一,抽象类:abstract 1,只要有一个或一个以上抽象方法的 ...
- Java几种常见的排序方法
日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一 ...
- ABBYY Cup 3.0 - Finals (online version)
A 开个数组记录一下 #include <iostream> #include<cstdio> #include<cstring> #include<algo ...
- Java实现求二叉树的路径和
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...