dfs枚举
class Solution { public: unordered_set<int> st; void search(vector<int> &num , vector<vector<int> > &ans , vector<int>& tmp , int dep ,int size) { if(dep >= size) { ans.push_back(tmp); return ; } for(int i = 0 ; i < size ; i++) { if (st.find(num[i]) == st.end()) { st.insert(num[i]); tmp.push_back(num[i]); search(num , ans , tmp , dep + 1 , size); tmp.pop_back(); st.erase(num[i]); } } } vector<vector<int> > permute(vector<int> &num) { sort(num.begin() , num.end()); st.clear(); vector<int> tmp; vector<vector<int> > ans; search(num , ans , tmp , 0 , num.size()); return ans; } };
by 1957