Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

] Ideas :
First , Fix the first number , Recursively find the full permutation of the ordered array , such as example in , The recursive procedure is : Fix 1, seek [2,3] The whole arrangement , Then fix 2, seek [3] The whole arrangement , Fix [3] seek , Full Permutation of empty arrays , Because the array is empty at this time , So save the results , return .
And then exchange seats , Continue recursion , It's not very clear , Just look at a picture . This is an enumeration tree . Now the question is how to come up 1,2,3 The beginning node ? It's using exchange , such as 1 and 2 In exchange for , obtain [2,1,3] Fix 2, seek [1,3] The whole arrangement .
How can I get it after the exchange 3 At the beginning ? Because it's recursive , So you can swap back 1,2 And then exchange 1,3 obtain 3 At the beginning .


class Solution {
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
permuteRec( 0, nums, result);
return result;
} private:
void permuteRec( int start, vector<int> &nums, vector<vector<int>> &result ){
if( start >= nums.size()){
for( int i = start; i < nums.size(); i++ ){
swap( nums[start], nums[i] );
permuteRec( start+1, nums, result);
swap( nums[start], nums[i] );

LeetCode 【46. Permutations】 More articles about

  1. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = '' 46: Permutations ...

  2. LeetCode:46. Permutations(Medium)

    1. Original link 2. Subject requirements Given an integer array nums, The numbers in the array are different from each other , Returns the number ...

  3. LeetCode 【47. Permutations II】

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

  4. 【 Line segment tree 】【4-6 Team games 】Problem H

    Problem Description #include <iostream> #include <algorithm> using namespace std; int n, ...

  5. LeetCode【 The first question is 】Two Sum

    Ready to brush LeetCode 了 . subject : ''' Given an array of integers, return indices of the two numbers such that they ...

  6. Some articles on Zhihu ---leetcode【 note 1】

    Zhang Tuwang Java{script} Code boy 42  People agree [1.19.2017] to update : 2017 year 1 month 17 Japan , I've been fighting with you for many years 2014 MackB ...

  7. leetcode【14 topic 】Longest Common Prefix

    subject :Longest Common Prefix Content : Write a function to find the longest common prefix string amongst an ar ...

  8. LeetCode【110. Balanced binary trees 】

    For balanced binary trees , It's the difference in depth between the left and the right 1 You can do another function , Calculate the depth , then , Whether the depth of comparison on the original function is different 1, Then the output true or false. As for iteration , After comparing the left and right nodes , Compare the left and right nodes . ...

  9. leetcode 【Rotate List 】python Realization

    subject : Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

Random recommendation

  1. kgcd ,fmod,fgcd

    Reference resources :NENU CS ACM Templates made by tiankonguse  2.13 GCD Fast gcd: I didn't learn bit operation , I really don't know binary , I still have to learn code: int kgcd(){ if(!a || ...

  2. C# Design patterns —— The state pattern (State Pattern)

    One . Overview in object-oriented software design , We often encounter that an object has different behaviors due to different states . If you use if else or switch case And so on , Object operation and object state are coupled together , Complex situations can lead to code structure ...

  3. Alibaba cloud :linux A key to install web Environmental Science

    Reference address :

  4. Jenkins - Build a continuous integration environment 【 turn 】

    1. Jenkins  summary Jenkins Is an open source continuous integration tool . The main function of continuous integration is to build automatically . Automated build includes automated compilation . Release and test , To find integration errors as quickly as possible , Enable teams to develop cohesive software faster . 2 ...

  5. [Python] Simple image processing (PIL or Pillow)

    I did it the other day django Upload your pictures , After uploading, you need to do some simple processing ,python in PIL Modules are designed to do this . So I did a few common picture operations , Record... Here , For backup . Here's a font file , Big ...

  6. Callable and Future、FutureTask Use ...

  7. Kali linux 2016.2(Rolling) In the installation OpenVAS

    Not much to say , Direct delivery of dry goods ! This blog post , Is in Kali 2.0 linux in , install OpenVAS. Preface OpenVAS Is an open vulnerability assessment tool , It is mainly used to detect the security of the target network or host . And security focus X-Scan Tools ...

  8. Tomcat java zabbix monitor

    The steps to get rid of Tom cat's mistakes ps-ef | grep java or jps –lvm see java pid process netstat –lntup | grep java see java The port has not been started see tomc ...

  9. linux shell Some orders wc: https: ...

  10. To configure siebel capture SQL sentence

    C:\Siebel\\Client\BIN\siebel.exe /c c:\Siebel\\Client\bin\chs\siebel.cfg /B &quo ...