My AC Solution in C++, I think this is not dfs, is it?


  • 7
    V
    class Solution {
    public:
    	vector<vector<int> > permute(vector<int> &num) {
    		vector<vector<int>> result;
    		if (num.size() == 0)
    			return result;
    		sort(num.begin(), num.end());
    		permute(num, 0, result);
    		return result;
    	}
    private:
    	void permute(vector<int> &num, size_t start, vector<vector<int>> &result){
    		if (start == num.size()-1){
    			result.push_back(num);
    		}
    		else{
    			for (size_t i = start; i < num.size(); i++){
    				swap(num[start], num[i]);
    				permute(num, start + 1, result);
    				swap(num[start], num[i]);
    			}
    		}
    	}
    };

  • 0
    S

    Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example


  • 0
    T

    This is backtracking.


  • 0
    J

    Yes, it is DFS. It's hard to tell the difference between DFS and Backtracking.


Log in to reply
 

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