Permutations I&&II
Permutations I
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
这里需要特别注意,第15行用的是index+1,而不是i+1这里也是与以前的代码思路不一样的地方,开始就是这里用了i+1,导致测试一直不通过,试了很久才发现这个错误。
class Solution {
private:
vector<vector<int>> res;
public:
void getPer(vector<int>&nums,int index,int lenth)
{
if(index==lenth)
res.push_back(nums);
int temp;
for(int i=index;i<lenth;i++)
{
temp=nums[i];
nums[i]=nums[index];
nums[index]=temp;
getPer(nums,index+,lenth);
temp= nums[i];
nums[i]=nums[index];
nums[index]=temp;
}
return ;
}
vector<vector<int>> permute(vector<int>& nums) {
getPer(nums,,nums.size());
return res; }
};
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]
.
这道题其实也纠结了我很久。没想到只需要定义一个set来存储已经交换过的元素值就可以把问题完美的解决了。
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
if(num.size() <= ) return res;
permCore(num, );
return res;
}
private:
vector<vector<int> > res;
void permCore(vector<int> &num, int st){
if(st == num.size()) res.push_back(num);
else{
set<int> swp;
for(int i = st; i < num.size(); ++i){
if(swp.find(num[i]) != swp.end()) continue;
swp.insert(num[i]);
swap(num, st, i);
permCore(num, st+);
swap(num, st, i);
}
}
} void swap(vector<int> &num, int left, int right){
int tmp = num[left];
num[left] = num[right];
num[right] = tmp;
}
};
Permutations I&&II的更多相关文章
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 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 ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
随机推荐
- C. Line (扩展欧几里得)
C. Line time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- logrotate配置和使用
logrotate是linux自带的日志管理工具.服务器如果不对日志进行滚动操作,单个日志文件的增长速度极快,不利于日志查找和问题定位.而logrotate能够自动完成日志的截断.压缩和滚动操作. 安 ...
- 深入探析 Rational AppScan Standard Edition 多步骤操作
序言 IBM Rational AppScan Standard(下文简称 AppScan)作为面向 Web 应用安全黑盒检测的自动化工具,得到业界的广泛认可和应用.很多人使用 AppScan 时都采 ...
- each jquery
<div class="first"> <span>投保人数:</span> <input type="text" i ...
- X210(s5pv210)中断系统
1.SoC对中断的实现机制:异常向量表 (1)异常向量表是CPU中某些特定地址的特定定义.当中断发生的时候,中断要想办法通知CPU去处理中断,怎么做到?这就要靠异常向量表.(2)在CPU设计时,就事先 ...
- c# 计算时间差---天数
---处理两个时间相差的天数 测试数据:三个时间 DateTime dt1 = Convert.ToDateTime("2017-03-17 09:49:55.667"); Dat ...
- vue-transition-move
<!Doctype> <html> <head> <meta charset="utf-8"> <meta name=&quo ...
- spring常用管理bean注解
spring提供了多个注解声明Bean为spring管理的Bean @Controller 声明此类是一个MVC类,通常与@RequestMapping一起使用 @Controller @Reques ...
- MySQL新建用户,授权
登录MySQL mysql -u root -p 添加新用户 允许本地 IP 访问 localhost, 127.0.0.1 create user 'test'@'localhost' identi ...
- hdu 3689 Infinite monkey theorem
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...