LeetCode Permutations


Given a collection of 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].

The original problem is here.

The original code is here.

My Solution

I solve this problem in C++, as below:

*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
using namespace std;

class Solution {
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> result;
        if(nums.size() <= 0)
            return result;
        findResult(nums, 0, result);
        return result;
    void findResult(vector<int>& nums, int i, vector<vector<int>> &result){
        int size = nums.size();
        if(i == size-1){
        for(int j=i;j<nums.size();j++){
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            findResult(nums, i+1, result);
            temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;


To solve the problem, recursion is needed. And, we need replace the current number with the numbers after the current.