leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0046.cpp (605B)


      1 class Solution {
      2     vector<vector<int>> res;
      3     vector<int> crnt;
      4     vector<bool> seen;
      5 
      6     void rec(vector<int> &nums, int n) {
      7         if (n == nums.size()) {
      8             res.push_back(crnt);
      9             return;
     10         }
     11         for (int i = 0; i < nums.size(); i++) {
     12             if (seen[i]) continue;
     13             seen[i] = true, crnt.push_back(nums[i]);
     14             rec(nums, n + 1);
     15             seen[i] = false, crnt.pop_back();
     16         }
     17     }
     18 
     19   public:
     20     vector<vector<int>> permute(vector<int> &nums) {
     21         seen.resize(nums.size());
     22         rec(nums, 0);
     23         return res;
     24     }
     25 };