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 ...
随机推荐
- javascript里面dom操作和兼容问题汇总
DOM 增,删,改,查 oUl.children 获取UL的子节点 有length的属性 oUl.children[0] 获取UL第1个子节点 使 ...
- c++面试题总结(1)
1.int a=5,则 ++(a++)的值是() A.5 B. 6 C.7 D.逻辑错误 a++返回的是一个临时变量,这里是右值,不能再前面++了 2.下面 ...
- JXSE and Equinox Tutorial, Part 2
http://java.dzone.com/articles/jxse-and-equinox-tutorial-part-0 ———————————————————————————————————— ...
- 使用「max-height」实现自适应高度
.tab-content{ max-height: 0; overflow: hidden; -webkit-transition: max-height .8s; -moz-transition: ...
- JavaScript 核心参考教程 内置对象
这个标准基于 JavaScript (Netscape) 和 JScript (Microsoft).Netscape (Navigator 2.0) 的 Brendan Eich 发明了这门语言,从 ...
- C#中的结构体与类的区别
经常听到有朋友在讨论C#中的结构与类有什么区别.正好这几日闲来无事,自己总结一下,希望大家指点. 1. 首先是语法定义上的区别啦,这个就不用多说了.定义类使用关键字class 定义结构使用关键字str ...
- IAR EWARM PRINTF/SCANF FORMATTER
The linker automatically chooses an appropriate formatter for printf- and scanf-related function bas ...
- Windows Self Signed Driver
In particular, Microsoft® instituted a device driver certification process for its Windows® desktop ...
- Vehicle’s communication protocol
http://www.crecorder.com/techInfo/commuProtocols.jsp COMMUNICATION PROTOCOLS A “communication protoc ...
- 六、Linux/UNIX操作命令积累【kill、netstat、df、du】
在使用Linux/UNIX下,常常会使用文本界面去设置系统或操作系统,作者本人在工作的过程也在不断接触这方面的命令,所以为此特酝酿.准备.開始了本文的编写. 本文主要记录自己平时遇到的一些Linux/ ...