LeetCode46,47 Permutations, Permutations II
题目:
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的更多相关文章
- LeetCode(47)Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- Permutations I&&II
Permutations I Given a collection of distinct numbers, return all possible permutations. For example ...
- 47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
随机推荐
- 了解shell
1. shell 脚本文件第一行: #!/bin/sh 或 #!/bin/bash "#!" 又称为纪数,在执行bash脚本的时候,内核会根据它来确定该用哪个程序来解释脚本 ...
- find 命令概览
Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...
- svg base64
好多h5页面有出现data:image/png;base64,后面跟了一串类似乱码的字母 查了下原来这也是svg或者是图片 <img src=“data:image/png;base64,iVB ...
- StructLayout特性
StructLayout特性 公共语言运行库利用 StructLayoutAttribute控制类或结构的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列.如果要将类传递 ...
- 用完成例程(Completion Routine)实现的重叠I/O模型
/// 用完成例程(Completion Routine)实现的重叠I/O模型 /// 异步IO模型 /// 用完成例程来实现重叠I/O比用事件通知简单得多.在这个模型中,主线程只用不停的接受连接 / ...
- 编译安装-PHP
一.编译配置选项2 配置帮助表:2 安装目录:2 交叉编译选项:2 特征选项:3 SAPI模块设置:3 普通参数设置:4 扩展参数:4 PEAR相关选项:9 ZEND相关选项:9 TSRM线程安全资源 ...
- Odoo constraints 使用教程
在日常开发Odoo的过程中,我们不免要用到Constraints,中文就是约束. 首先我们来介绍下Odoo里面的两种Constraints. SQL Constraints:就是添加一个数据库的约束. ...
- SNMP MIB中的含read-create节点的表的实现
做过snmp/mib开发的知道,常见的节点类型一般只有no-accessible,read-only,read-write三种访问类型.snmp V2中引入了一种新的访问类型:read-create. ...
- ADT下开发环境的配置--个人配置啦 Eclipse Color Themes
一. Eclipse Color Themes的安装 首先 这个ADT没有Marketplace Client 需要装一个, 节选自: http://blog.csdn.net/liu37226700 ...
- Tun/Tap interface tutorial
Foreword: please note that the code available here is only for demonstration purposes. If you want t ...