# 1 题目描述

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

# 3 源代码

package com.larry.easy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RotateArray {
public void rotate(int[] nums, int k) {
int len = nums.length;
if(k >= len) k %= len;

List<Integer> list = new ArrayList<Integer>();
for(int i = len-k; i < len; i++)
for(int i = 0; i < len-k; i++)
nums[len-1-i] = nums[len-k-i-1];
for(int i = 0; i < k; i++)
nums[i] = list.get(i);
}

public static void main(String[] args) {
/*int[] nums = {1,2,3,4,5,6,7};
int k = 3;*/
/*int[] nums = {1,2};
int k = 3;*/
int[] nums = {1,2,3};
int k = 1;
RotateArray ra = new RotateArray();
ra.rotate(nums, k);
System.out.println(Arrays.toString(nums));

}
}


#### [leetcode][array] Rotate Array

2015-05-23 13:41:10

#### rotate array

2015-03-07 19:42:51

#### [Array]Rotate Array

2016-07-08 12:47:12

#### Array(4) -- Rotate Array

2016-12-15 22:40:58

#### LeetCode(189) Rotate Array

2015-03-20 12:24:02

#### Leetcode 189: Rotate Array

2015-03-06 07:51:14

#### Rotate Array数组旋转

2016-08-17 21:37:05

#### 【LeetCode】Rotate Array 旋转数组

2015-04-23 15:38:43

#### 1334. Rotate Array

2018-06-17 17:24:54

#### 189. Rotate Array (python)

2016-10-30 16:09:17