//rotate_right publicstaticvoidtripleReverse(int[] nums, int K){ int length = nums.length; reverse(nums, 0, length - K - 1); reverse(nums, length - K, length - 1); reverse(nums, 0, length - 1); }
privatestaticvoidreverse(int[] nums, int start, int end){ int sum = start + end; int temp; for (int i = start; i <= (start + end) / 2; ++i) { temp = nums[i]; nums[i] = nums[sum - i]; nums[sum - i] = temp; } }
//rotate_right publicstaticvoiddoubleSwap(int[] nums, int K){ int start = 0; int end = nums.length; int middle = nums.length - K; int i = middle; int temp;
while (true) { temp = nums[start]; nums[start] = nums[i]; nums[i] = temp; ++start; if (++i == end) { if (start == middle) { break; } i = middle; } elseif (start == middle) { middle = i; } } }
// rotate left // if want to rotate right, add [K = nums.length - K] at the beginning publicstaticvoidrotate_GCD(int nums[], int K){ int length = nums.length; int front, post, temp; for (int i = 0; i < GCD(length, K); ++i) { temp = nums[i]; front = i; while (true) { post = front + K; if (post >= length) { post = post - length; } if (post == i) { break; } nums[front] = nums[post]; front = post; } nums[front] = temp; } }
privatestaticintGCD(int a, int b){ if (b == 0) { return a; } else { return GCD(b, a % b); } }
publicstaticvoidmain(String[] args){ // 一亿个整数,大概需要三百多兆的内存 int[] array = newint[1_0000_0000]; for (int i = 0; i < 1_0000_0000; ++i) { array[i] = i; }
long startTime; long durationNanoSecond; double durationMicroSecond;