13ms C++


  • 0
    K

    vector<vector<int> > permute(vector<int> &num) {
    vector<vector<int> > result;
    sort(num.begin(), num.end());
    int len = num.size();
    int count = 1;
    while (len){
    count *= len--;
    }
    result.push_back(num);
    for (int i = 0; i < count-1;i++){

    	int pre = num.size() - 2, next = num.size() - 1;
    	while (pre>=0&&num[pre]>=num[pre+1]){
    		pre--;
    	}
    	if (pre < 0)
    		pre = 0;
    	while (next >= 0&& num[next] < num[pre]){
    		next--;
    	}
    	swap(num[pre], num[next]);
    	reverse(num.begin()+pre+1,num.end());
    	result.push_back(num);
    
    }
    return result;
    

    }


Log in to reply
 

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