# 189. Rotate Array

{% embed url="<https://leetcode.com/problems/rotate-array/>" %}

{% tabs %}
{% tab title="Python" %}

```python
# python solution 1:
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        for i in range(k):
            s = nums.pop(-1)
            nums.insert(0, s)
```

{% endtab %}

{% tab title="Java" %}

```java
class Solution {
    public void rotate(int[] nums, int k) {
        if (nums.length==0 || k%nums.length==0) return;
        
        int n = nums.length;
        k %= n;
        
        reverse(nums, 0, n);
        reverse(nums, k, n);
        reverse(nums, 0, k);
    }
    private static void reverse(int[] nums, int start, int end) {
        for (int i=start, j=end-1; i<j; i++, j--) {
            int step = nums[i];
            nums[i] = nums[j];
            nums[j] = step;
        }
    }
}
```

{% endtab %}
{% endtabs %}
