> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/189.-rotate-array.md).

# 189. Rotate Array

## 1.問題

![](/files/-LKX0GVrZZ_iay6YMl7o)

## 2.想法

## 3.程式碼

```
class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        if (k == 0)
        {
            return;
        }
        k = k % nums.size();
        for (int i = 0 ; i < k ; i++)
        {
            nums.insert(nums.begin(), nums.back());
            nums.pop_back();
        }
    }
};
```

## 4.Performance

![](/files/-LKX0rhS93rmCYNE_Jmv)
