aboutsummaryrefslogtreecommitdiff
path: root/permutations.cpp
blob: 2e86fc02d35d55f4b0169a4f7690a50231a00ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "leetcode.h"

class Solution {
public:
  vector<vector<int>> permute(vector<int> &nums) {
    vector<vector<int>> ans;
    int idx = 0;
    helper(nums, ans, idx);
    return ans;
  }

private:
  void helper(vector<int> &nums, vector<vector<int>> &ans, int idx) {
    if (idx >= nums.size()) {
      ans.push_back(nums);
      return;
    }
    for (int i = idx; i < nums.size(); i++) {
      swap(nums[idx], nums[i]);
      helper(nums, ans, idx + 1);
      swap(nums[idx], nums[i]);
    }
  }
};

int main() {}