Question

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Thinking:

  • Method: O(N) extra space, O(N) time cost
class Solution {
    public void rotate(int[] nums, int k) {
        if(nums == null || nums.length == 0) return;
        int len = nums.length;
        k = k % len;
        int[] temp = new int[k];
        int i = len - k;
        for(; i < len; i++)
            temp[i - (len - k)] = nums[i];
        for(i = len - k - 1; i >= 0; i--)
            nums[i + k] = nums[i];
        for(i = 0; i < k; i++)
            nums[i] = temp[i];
    }
}
  • Method 2:
    • 整体反转
    • 前K反转
    • 后面反转
class Solution {
    public void rotate(int[] nums, int k) {
        int len = nums.length;
        k %= len;
        rotate(nums, 0, len - 1);
        rotate(nums, 0, k - 1);
        rotate(nums, k, len - 1);
    }
    private void rotate(int[] nums, int left, int right){
        while(left < right){
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
    }
}

二刷

  1. 先计算余数。
  2. 保存从尾向前余数个个数。
  3. 将剩余的数向后以后。
  4. 最后将保存的数填充到数组的最开头。
class Solution {
    public void rotate(int[] nums, int k) {
        int len = nums.length;
        int t = k % len;
        int[] arr = new int[t];
        for(int i = 0; i < t; i++){
            arr[i] = nums[len - t + i];
        }
        for(int i = len - t - 1; i >= 0; i--){
            nums[i + t] = nums[i];
        }
        for(int i = 0; i < t; i++)
            nums[i] = arr[i];
    }
}

Amazon session

  • Method 1:with O(n) extra space
      class Solution {
          public void rotate(int[] nums, int k) {
              if(nums == null || nums.length == 0) return;
              int len = nums.length - k % nums.length;
              int[] temp = new int[len];
              int i = 0;
              for(; i < len; i++){
                  temp[i] = nums[i];
              }
              for(; i < nums.length; i++){
                  nums[i - len] = nums[i];
              }
              int count = 0;
              while(count < len){
                  nums[nums.length - 1 - count] = temp[len - 1 - count];
                  count++;
              }
          }
      }
    
  • Method 2: with O(k) extra space
      class Solution {
          public List<Integer> getRow(int rowIndex) {
              List<Integer> res = new ArrayList<>();
              res.add(1);
              if(rowIndex == 0){
                  return res;
              }else if(rowIndex == 1){
                  res.add(1);
                  return res;
              }
              int[] result = new int[rowIndex + 1];
              result[0] = 1;
              result[1] = 1;
              for(int i = 2; i <= rowIndex; i++){            
                  int pre = 1;
                  for(int j = 1; j <= i; j++){
                      int copy = result[j];
                      result[j] = pre + result[j];
                      pre = copy;
                  }
    				
              }
              List<Integer> list = new ArrayList<>();
              for(int n : result) list.add(n);
              return list;
          }
      }