https://oj.leetcode.com/problems/permutations/

写出一列数的全排列

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution{
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > ans;
if(num.size()==)
return ans; vector<int> _num = num;
sort(_num.begin(),_num.end()); ans.push_back(_num);
while(nextPermutation(_num))
{
ans.push_back(_num);
}
return ans;
}
private:
bool nextPermutation(vector<int> &num)
{
return next_permutation(num.begin(),num.end());
} template<typename BidiIt>
bool next_permutation(BidiIt first,BidiIt last)
{
const auto rfirst = reverse_iterator<BidiIt>(last);
const auto rlast = reverse_iterator<BidiIt>(first); auto pivot = next(rfirst);
while(pivot != rlast && *pivot >= *prev(pivot))
{
++pivot;
} //this is the last permute, or the next is the same as the begin one
if(pivot == rlast)
{
reverse(rfirst,rlast);
return false;
}
//find the first num great than pivot
auto change = rfirst;
while(*change<=*pivot)
++change; swap(*change,*pivot);
reverse(rfirst,pivot);
return true;
}
}; int main()
{
vector<int> num;
num.push_back();
num.push_back();
num.push_back(); Solution myS;
myS.permute(num);
return ;
}

LeetCode OJ--Permutations *的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. Java for LeetCode 047 Permutations II

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

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  10. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. loj6485 LJJ 学二项式定理

    题目描述: loj 题解: 单位根反演. $[n|x]=\frac{1}{n} \sum _{i=0}^{n-1} (ω_n^x)^i$ 证明?显然啊,要么停在$(1,0)$要么转一圈. 所以说题目要 ...

  2. 【dp】守望者的逃离

    妙 题目描述 恶魔猎手尤迪安野心勃勃,他背着了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快 ...

  3. redis学习笔记(3)

    redis学习笔记第三部分 --redis持久化介绍,事务,主从复制 三,redis的持久化 RDB(Redis DataBase)AOF(Append Only File) RDB:在指定的时间间隔 ...

  4. java中类与对象的概念(2013-05-04-bd 写的日志迁移

    1:类是抽象的,概念的,代表一类事物,比如人类.猫类.. 2:对象是具体的,实际的,代表一个具体的事物 3:类是对象的模板,对象是类的一个个体,实例 创建对象的两种方法: 1.先声明在创建 对象声明: ...

  5. 面试之mybatis和hibernate的区别

    mybatis是支持普通SQL查询.存储过程和高级映射的优秀持久层框架.封装了 几乎所有的JDBC代码和参数的手工设置 ,以及结果集的检索: 封装了:1,获取连接,执行sql,释放连接. 2,sql的 ...

  6. 1.python中的变量

    什么是变量 1.在任何语言中都有变量的概念,在python中变量是用一个变量名表示,变量名必须是用大小写英文字母,数字,下滑写(_)组成.不能用数字开头.(但用中文做变量名也可以,不要这样做) 例: ...

  7. selection problem-divide and conquer

    思路: 随机选取列表中的一个值v,然后将列表分为小于v的,等于v的,大于v的三组.对于k<=left.size()时, 在left中执行selection:落在中间的,返回v:k>left ...

  8. LA 4253 Archery 二分

    题意: x轴上方有若干条平行于x轴的线段 在x轴的区间\([0, \, W]\)内找一点发射一条射线,使其穿过所有线段. 问是否存在这样的点. 分析: 我们二分射线端点的坐标,将线段按纵坐标从小到大排 ...

  9. JSON Undefined 问题

    在IE6和IE7浏览器下或在IE8-IE10浏览器文档模式为IE7及以下时,控制台会报错:JSON is undefined. 这种错误在IE6和IE7浏览器下出现很正常,因为JSON在IE8+浏览器 ...

  10. webdriver高级应用- 启动FireFox的同时打开Firebug

    1. 首先本机Firefox浏览器需要安装一下firebug插件,具体怎么安装这里不赘述,网上教程很多. 2. 具体自动化实现的代码如下: #encoding=utf-8 from selenium ...