LeetCode Permutations
class Solution { private: vector<vector<int> > result; public: vector<vector<int> > permute(vector<int> &num) { result.clear(); dfs(num, 0); return result; } void dfs(vector<int>& num, int idx) { int tmp; int len = num.size(); if (len == idx + 1) { result.push_back(num); return; } for (int i=idx; i<len; i++) { tmp = num[idx]; num[idx] = num[i]; num[i] = tmp; dfs(num, idx + 1); tmp = num[idx]; num[idx] = num[i]; num[i] = tmp; } } };
重复的情况竟然可以,又水了一发