题目:

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. Orion Network Performance Monitor 软件在网络管理中的应用

    Orion Network Performance Monitor 软件在网络管理中的应用       Orion Network Performance Monitor是完全的带宽性能和故障管理软件 ...

  2. 提升c++builder 代码输入流畅度的配置

    提高c++builder 代码输入流畅度 1.输入指针的函数名后,识别函数参数移动光标到括弧内,此功能太慢,有明显延迟,建议关闭.关闭以后,输入函数名不会自动添加(),需要自己手动输入括弧了,不过速度 ...

  3. 【转】Java transient关键字

    Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...

  4. UVALive 7077 - Little Zu Chongzhi's Triangles(暴力)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. hdu 5491 The Next (位运算)

    http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...

  6. RGPJS 教程之八 创造场景

    开始画面 游戏画面 代码 <!DOCTYPE html> <html> <head> <script src="rpg-beta-2.js" ...

  7. php连接mysql配置

    php连接mysql测试代码: <?php $link=mysql_connect('localhost','root','123456'); if(!$link) echo "失败! ...

  8. Tair分布式key/value存储

    [http://www.lvtao.net/database/tair.html](特别详细)   tair 是淘宝自己开发的一个分布式 key/value 存储引擎. tair 分为持久化和非持久化 ...

  9. vtk点云数据的显示[转]

    #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h& ...

  10. Backbone

    app.js作为backbone 业务代码主模块,内容很简单,在页面加载完之后,对AppView进行了实例化