Beat 100% 26ms cpp code


  • 0
    T
    vector<int> nextPermutation(vector<int> nums){
            const int N = nums.size();
            int i = N-1;
            while(i > 0 && nums[i] <= nums[i-1]) i--;
            if(i <= 0) return vector<int>();
            const int NSIZE = N - i;
            for(int j = 0; j < NSIZE/2; ++j){
                int tmp = nums[i+j];
                nums[i+j] = nums[i+NSIZE-j-1];
                nums[i+NSIZE-j-1] = tmp;
            }
            int l(i), r(N);
            while(l < r){
                int mid = (l + r) / 2;
                if(nums[mid] <= nums[i-1])
                    l = mid + 1;
                else
                    r = mid;
            }
            int tmp = nums[i-1];
            nums[i-1] = nums[l];
            nums[l] = tmp;
            return nums;
        }
    
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            vector<vector<int>> res;
            res.push_back(nums);
            vector<int> nextPer = nextPermutation(nums);
            while(!nextPer.empty()){
                res.push_back(nextPer);
                nextPer = nextPermutation(nextPer);
            }
            return res;
            
        }

Log in to reply
 

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