Recover Rotated Sorted Array
public void recoverRotatedSortedArray(ArrayList<Integer> nums) { // write your code for(int i = 0; i < nums.size()-1; i++){ if(nums.get(i) > nums.get(i+1)){ reverse(nums,0,i); reverse(nums,i+1,nums.size()-1); reverse(nums,0,nums.size()-1); break; } } } public void reverse(ArrayList<Integer> nums, int i, int j) { while(i < j) { Integer tmp = nums.get(i); nums.set(i,nums.get(j)); nums.set(j,tmp); i++; j--; } }
Leave A Comment