class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i, j;
if(nums.size() <= 1)
return;
for(i = nums.size()-2; i >= 0; i--){ //找左标记
if(nums[i] < nums[i + 1]){
break;
}
}
for(j = nums.size()-1; j > i && i != -1; j--){ //找右标记
if(nums[i] < nums[j]){
swap(nums[i], nums[j]);
break;
}
}
reverse(nums.begin() + i + 1, nums.end());
}
};
Accepted
265/265 cases passed (4 ms)
Your runtime beats 72.39 % of cpp submissions
Your memory usage beats 94.62 % of cpp submissions (11.6 MB)