Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public void nextPermutation(int[] nums) {
- int startSort = 0;
- boolean swaped = false;
- for(int i = nums.length-2; i >= 0; i--){
- if(nums[i] < nums[i+1]){
- startSort = i+1;
- for (int j = nums.length-1; j > startSort ; j--){
- if(nums[i] < nums[j]){
- swap(nums, i, j);
- swaped = true;
- break;
- }
- }
- if(!swaped){
- swap(nums, i, startSort);
- }
- break;
- }
- }
- int[] arr = new int[nums.length - startSort];
- System.arraycopy(nums, startSort, arr,0, nums.length - startSort);
- Arrays.sort(arr);
- for(int i = 0; i < arr.length; i++){
- nums[i+startSort] = arr[i];
- }
- }
- public void swap(int[] nums, int i, int k){
- int temp = nums[i];
- nums[i] = nums[k];
- nums[k] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement