image.png

解题思路

使用反转

image.png

  1. public class Solution {
  2. public void rotate(int[] nums, int k) {
  3. k %= nums.length;
  4. reverse(nums, 0, nums.length - 1);
  5. reverse(nums, 0, k - 1);
  6. reverse(nums, k, nums.length - 1);
  7. }
  8. public void reverse(int[] nums, int start, int end) {
  9. while (start < end) {
  10. int temp = nums[start];
  11. nums[start] = nums[end];
  12. nums[end] = temp;
  13. start++;
  14. end--;
  15. }
  16. }
  17. }