1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| class Solution { public void nextPermutation(int[] nums) { int n = nums.length; if (n <= 1){ return; } boolean b = true; for (int i = n-1; i >= 0 ; i--) { int k = -1; for (int j = i+1; j <= n - 1 ; j++){ if (nums[j] > nums[i]){ if (k == -1 || nums[j] < nums[k]){ k = j; } } }
if (k != -1){ int tmp = nums[k]; nums[k] = nums[i]; nums[i] = tmp;
Arrays.sort(nums, i+1, n); return; } }
int i = 0; int j = n - 1; while (true){ if (i >= j) { break; }
int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; i++; j--; } } }
|