Created
December 9, 2018 03:04
-
-
Save zhangxiaomu01/1e7353d1fbb425215ea2e353956b7e3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
void nextPermutation(vector<int>& nums) { | |
int i = nums.size() - 2; | |
while(i >= 0 && nums[i+1] <= nums[i]) | |
i--; | |
if(i>=0){ | |
int j = nums.size() - 1; | |
while(j >= i+1 && nums[j] <= nums[i]) | |
j--; | |
swap(nums[i],nums[j]); | |
} | |
reverse(nums.begin()+i+1, nums.end()); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment