c++ concise backtack solution.


  • 1
    class Solution {
    public:
      vector<vector<int> > permute(vector<int>& nums) {
        vector<vector<int> > ret;
        backtack(ret, nums, 0);
        return ret;
      }
      void backtack(vector<vector<int> > &ret, vector<int> &nums, int index)
      {
        if (index == nums.size())
        {
          ret.push_back(nums);
        }
        else
        {
          for (int i = index; i < nums.size(); i ++)
          {
    	swap(nums[index], nums[i]);
    	backtack(ret, nums, index + 1);
    	swap(nums[index], nums[i]);
          }
        }
      }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.