一天一道LeetCode系列

(一)题目

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].

(二)解题

求全排列数。具体思路可以参考【一天一道LeetCode】#31. Next Permutation这篇博客。



class Solution {

public:

    vector<vector<int>> permute(vector<int>& nums) {

        vector<vector<int>> ret;

        sort(nums.begin(),nums.end());

        do{

            ret.push_back(nums);

        }while(nextpermute(nums));

        return ret;

    }

    bool nextpermute(vector<int>& nums)

    {

        int i = nums.size() -2;

        while(i>=0 && nums[i] > nums[i+1]) i--;//找到第一个破坏升序的数,

        int j = nums.size()-1;

        while(j>=0 && nums[j] < nums[i]) j--;//找到第一个大于i的数

        if(i>=0)

        {

            swap(nums[i],nums[j]);//交换i和j

            reverse(nums.begin()+i+1,nums.end());//将i以后的数反转

            return true;//如果还存在下一个全排列数就返回true

        }

        return false;//如果数组已经为倒序了就说明没有下一个了,返回false

    }

};

【一天一道LeetCode】#46. Permutations的更多相关文章

  1. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  2. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  3. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

  4. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

  5. [LeetCode] 47. Permutations II 全排列 II

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

  6. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  7. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  8. 【一天一道LeetCode】索引目录 ---C++实现

    [一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...

  9. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. hadoop入门级总结一:HDFS

    虽然hadoop经历了多年的发展,作为技术人员都或多或少的使用过或者了解过.这里还是做一个简单的总结,主要原因是之前主要是做hadoop的开发,对hadoop的运维知之甚少,但真正的接触到hadoop ...

  2. 剑指Offer——知识点储备-常用算法

    剑指Offer--知识点储备-常用算法 快速排序 注:若排序是有序的,采用快排,则退化为冒泡排序. 解决这个问题,采用两个选取基准的方法 (1)随机选取基数(在这个区间内随机取一个数) 出现的恶劣情况 ...

  3. Java异常处理-----运行时异常(RuntimeException)

    RuntimeException RunntimeException的子类: ClassCastException 多态中,可以使用Instanceof 判断,进行规避 ArithmeticExcep ...

  4. Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)

    讲这个之前,我们先来说说get和post两种请求的区别吧!!! 1. GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditPosts.jsp?name=te ...

  5. JDBC的java驱动安装

    首先登陆mysql.com官方网站,download-->选中下面的community–>mysql connentor-->然后选中下面与平台无关的zip包,一般是第二个,完成下载 ...

  6. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. 08 ListView 优化的例子

    package com.fmy.homework; import java.util.List; import com.fmy.homework.httputil.HttpUtil; import c ...

  8. UNIX网络编程——使用select函数的TCP和UDP回射服务器程序

    服务器程序: #include <sys/wait.h> #include <string.h> #include <string.h> #include < ...

  9. hadoop端口使用配置总结(非常好的总结)

    转自http://www.aboutyun.com/thread-7513-1-1.html Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以 ...

  10. python的u'字符串"(字符编码):字符串前有u,表示字符串以unicode格式存储

    举个例子 >>> s = u'\u6ce8\u91ca' >>> s u'\u6ce8\u91ca' >>> print s 注释 >> ...