12ms C++ solution


  • 0
    I
    bool help(int start, vector<int>& nums) {
        if (start == nums.size() - 1) return false;
        if (help(start + 1, nums)) return true;
        for (int i = start + 1; i < nums.size(); i++) {
            if (nums[start] < nums[i]) {
                swap(nums[start], nums[i]);
                return true;
            }
        }
        int temp = nums[start];
        for (int i = start + 1; i < nums.size(); i++) nums[i - 1] = nums[i];
        nums.back() = temp;
        return false;
    }
    
    void nextPermutation(vector<int>& nums) {
        help(0, nums);
    }

Log in to reply
 

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