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的更多相关文章

  1. [LeetCode] Permutations II 全排列之二

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

  2. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  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. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  6. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  7. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  8. Permutations,Permutations II,Combinations

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

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

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

随机推荐

  1. BAT-Java必考面试题集

    2018最新<BAT Java必考面试题集> 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象 ...

  2. selenium测试-open chrome

    通过selenium来打开浏览器测试之前,需要确认本地已安装相应的webdriver,本例以chrome为例. 1. 查看本地chrome版本,以此确认需要安装的webdriver版本 查看chrom ...

  3. Blender绘制大脑表层,并高亮染色

    首先, 有必要熟悉一下Blender的一些快捷键.(实在不想吐槽Blender反人类的交互操作了) 按鼠标右键是选择某个物体.(是右键,而不是左键!) 按A键,取消选中或者选中全部物体. 按H键,隐藏 ...

  4. C#泛型实例详解

    本文以实例形式讲述了C#泛型的用法,有助于读者深入理解C#泛型的原理,具体分析如下: 首先需要明白什么时候使用泛型: 当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型. 一.针 ...

  5. POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

    Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...

  6. II8部署WCF服务出错

    环境:Windows 2012 R2 + IIS 8.0 + .NET 4.5 错误404.3 - Not Found: 控制面板->程序->启用或关闭Windows功能,如下图所示,将需 ...

  7. Farey Sequence (欧拉函数+前缀和)

    题目链接:http://poj.org/problem?id=2478 Description The Farey Sequence Fn for any integer n with n >= ...

  8. NYOJ 1237 最大岛屿 (深搜)

    题目链接 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王.  这是一个由海洋. ...

  9. 【ALB学习笔记】基于多线程方式的串行通信接口数据接收案例

    基于多线程方式的串行通信接口数据接收案例 广东职业技术技术学院  欧浩源 1.案例背景 在本博客的<[CC2530入门教程-06]CC2530的ADC工作原理与应用>中实现了电压数据采集的 ...

  10. perl发送post数据

    把post数据写进一个匿名数组里就行 #!/usr/bin/env perl -w use strict; use LWP::UserAgent; my $ua = LWP::UserAgent-&g ...