自己的暴力(?)算法问题主要在于没有在循环的过程中重置临时变量,导致出现的问题,贴代码
class Solution { public: vector<vector<int>> res; vector<vector<int>> permute(vector<int>& nums) { vector<int> res_temp; generate(nums,res_temp); return res; } void generate(vector<int> int_in,vector<int> res_in) { for(auto it = int_in.begin() ; it < int_in.end(); ++it) { vector<int> temp = int_in; vector<int> res_temp = res_in; res_temp.push_back(*it); if(temp.size() == 1) { res.push_back(res_temp); } else { temp.erase(find(temp.begin(),temp.end(),*it)); generate(temp,res_temp); } } } };