题目:

LeetCode46 I

Given a collection of distinct numbers, return all possible permutations. (Medium)

For example,
[1,2,3] have the following permutations:

[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

LeetCode47 II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.(Medium)

For example,
[1,1,2] have the following unique permutations:

[
[1,1,2],
[1,2,1],
[2,1,1]
]

分析: 首先先用next_permutation解这两个题。用好STL。

Permutations I:

 class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
int len = nums.size();
sort(nums.begin(), nums.end());
do {
result.push_back(nums);
} while (next_permutation(nums.begin(),nums.end())); return result;
}
};

Permutations II:

 class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result;
int len = nums.size();
sort(nums.begin(), nums.end());
do {
result.push_back(nums);
} while (next_permutation(nums.begin(),nums.end())); return result;
}
};

当然,用STL写好的函数肯定不是面试的目的,permutation的递归方法也是经典的回溯算法题。

代码:

 class Solution {
private:
vector<vector<int>> result;
void helper(vector<int>& nums, int start, int end) {
if (start == end) {
result.push_back(nums);
}
for (int i = start; i <= end; ++i) {
swap(nums[start], nums[i]);
helper(nums, start + , end);
swap(nums[start], nums[i]);
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
helper(nums, , nums.size() - );
return result;
}
};

LeetCode46,47 Permutations, Permutations II的更多相关文章

  1. LeetCode(47)Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  2. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  3. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  4. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  5. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

  6. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

  7. Permutations I&&II

    Permutations I Given a collection of distinct numbers, return all possible permutations. For example ...

  8. 47 Majority Element II

    原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...

  9. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

随机推荐

  1. 第二百零二天 how can I 坚持

    最近增肥好明显,胃口好没办法,只要肚子起不来就行了.加油. 其实挺幸福,想吃啥吃啥. 鱼会不会被冻死,买了加热棒不想用,该咋办呢. 股市又跌没了一千多,还是不够睿智,不够淡定. 人活这一辈子,到底最想 ...

  2. 简单版问卷调查系统(Asp.Net+SqlServer2008)

    1.系统主要涉及以下几个表  问卷项目表(Q_Naire) 问卷题目表(Q_Problem) 题目类型表(Q_ProblmeType) 题目选项表(Q_Options) 调查结果表(Q_Answer) ...

  3. Good Number

    Time Limit: 1000ms Problem Description: Let's call a number k-good if it contains all digits not exc ...

  4. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  5. V7承保 bug代码

    v7 bug1

  6. tomcat的 JNDI 配置

    tomcat的conf/server.xml 配置 尽量用简单版 <Context path="/cas" docBase="D:\YC\zqV7\cas\WebR ...

  7. LabVIEW数据记录和存储—XML文件

    XML(eXtensible Markup Language)是一种目前广泛使用的数据传输和存储的格式,其本质上是一种文本文件,可以使用任何一个文本编辑工具打开和修改.类似于HTML,XML被设计为具 ...

  8. class dict

    class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dic ...

  9. 使用UIGestureRecognizer监听屏幕事件

    转载自  http://blog.csdn.net/samguoyi/article/details/7911499 如果只是想获取屏幕点击事件有一个最简单的办法,就是写一个透明的uibutton覆盖 ...

  10. UVA 11983 Weird Advertisement(线段树求矩形并的面积)

    UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...