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 46 47 48 49 50
| class Solution { public void nextPermutation(int[] nums) { int len = nums.length; if(len < 2) return; int i =len -1; while(i>0 && nums[i-1] >= nums[i]){ i--; } reverse(nums,i,len-1); if(i==0)return; int j = i-1; while(i < len && nums[j] >= nums[i]){ i++; } int temp = nums[j]; nums[j] =nums[i]; nums[i] = temp;
}
public void reverse(int[] nums,int left,int end){ while(left < end){ int temp = nums[end]; nums[end] =nums[left]; nums[left] = temp; end--; left++; } } }
|