Analysis:
- find
j
in array in reverse order that nums[j]<nums[j+1]- find the first element that nums[i]>nums[j]
- swap the elements nums[i], nums[j]
- reverse nums[j+1:]
Time Complexity:
- O(n)
Space Complexity:
- O(1)
Code
1 | class Solution(object): |