Sharing my 16ms C++ simple solution


  • 0
    T
    class Solution {
    public:
        vector<vector<int>> permute(vector<int>& nums) {
            vector<int> index;
            int n = nums.size();
            int i;
            for(i=0; i<n; i++)
                index.push_back(i);
            vector<int> temp(n);
            vector< vector<int> > result;
            do
            {
                for(i=0; i<n; i++)
                    temp[i] = nums[index[i]];
                result.push_back(temp);
            }while(next_permutation(index.begin(), index.end()));
            return result;
        }
    };

Log in to reply
 

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