Great. I did almost the same thing, but I used binary search for the second scan. However, I failed to realize that I can simply reverse the non-ascending part, so I called `Arrays.sort`

instead, thus blowing up the complexity to O(n log n). Thanks to your solution, I have improved my run time from 3 ms to 2 ms (for some reason it's slower than linear search). Here is the code:

```
public void nextPermutation(int[] nums) {
if (nums.length <= 1) {
return;
}
int swap = -1;
for (int i = nums.length - 2; i >= 0; --i) {
if (nums[i] < nums[i + 1]) {
swap = i;
break;
}
}
if (swap >= 0) {
int min = nums[swap];
int l = swap + 1, r = nums.length - 1;
while (l <= r) {
int m = (l + r) >>> 1;
if (min >= nums[m]) { // "min >=" means "min + 1/2 >"
r = m - 1;
} else {
l = m + 1;
}
}
nums[swap] = nums[r];
nums[r] = min;
reverse(nums, swap + 1, nums.length);
} else {
reverse(nums, 0, nums.length); // non-descending
}
}
private void reverse(int[] nums, int start, int end) {
for (int i = start, j = end - 1; i < j; ++i, --j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
```