# Rotate Array

## Desicription

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]```

Note:

• Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
• Could you do it in-place with O(1) extra space?

## Solution

```class Solution {
private:
void fun(vector<int>& nums, int left, int right) {
if(left >= right)
return ;
while(left <= right) {
swap(nums[left], nums[right]);
left++;
right--;
}
}
public:
void rotate(vector<int>& nums, int k) {
k = k % nums.size();
fun(nums, 0, nums.size() - 1);
fun(nums, 0, k - 1);
fun(nums, k, nums.size() - 1);
}
};```

0 条评论

• ### Leetcode: Rotate Array

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

• ### Leetcode 189. Rotate Array

版权声明：博客文章都是作者辛苦整理的，转载请注明出处，谢谢！ https://blog.csdn....

• ### Leetcode 189 Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7...

• ### leetcode-189-Rotate Array

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

• ### leetcode 189 Rotate Array

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

• ### LeetCode 189. Rotate Array

用O(1)空间的算法去解决。找到nums[x]旋转之后对应的位置，交换二者，然后得到的数字再找下去。知道找到一开始的数字，形成一个闭环了，然后x++，直到交换次...

• ### LeetCode笔记：189. Rotate Array

旋转本身没太多特别好说的，我的做法是用另一个数组来记录旋转后的内容，然后复制回原数组。当然记录时是从第nums.length-k个元素开始记录，记录到末尾后再去...

• ### 挑战程序竞赛系列（11）：2.5最短路径

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### Array - 189. Rotate Array

189、Rotate Array Given an array, rotate the array to the right by k steps, wher...

• ### LeetCode 189：旋转数组 Rotate Array

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

• ### LeetCode 189：旋转数组 Rotate Array

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

• ### 关关的刷题日记12——Leetcode 189. Rotate Array 方法1、2、3

关小刷刷题12 – Leetcode 189. Rotate Array 方法1、2、3 题目 Rotate an array of n elements to...

• ### Q189 Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 a...

• ### 【LeetCode】数组--旋转数组（189）

关注较早的读者可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行，之前的【贪心算法】已经结束，虽然只有三个题却包含了简单，中等，困难这三个维度，今天...

• ### Array - 48. Rotate Image

You are given an n x n 2D matrix representing an image.

• ### LeetCode 189. 旋转数组（环形替换）

来源：力扣（LeetCode） 链接：https://leetcode-cn.com/problems/rotate-array 著作权归领扣网络所有。商业...

• ### 程序员面试金典 - 面试题 10.03. 搜索旋转数组（二分查找）

搜索旋转数组。给定一个排序后的数组，包含n个整数，但这个数组已被旋转过很多次了，次数不详。 请编写代码找出数组中的某个元素，假设数组元素原先是按升序排列的。若...

• ### C语言中的复制函数（strcpy和memcpy）第一章

PS：初学算法，开始刷leetcode，Rotate array的预备知识（写的代码Time Limit Exceed）于是百度高效算法，本篇作为预备知识。

• ### Leetcode 题目列表（难度、出现频率、知识点）

不全，但好像没看到有更好的版本，刷前132题暂时凑合着用吧！ 转载自：LeetCode Question Difficulty Distribution ?...

### Reck Zhang

TencentGame Developer

Tencent · Game Developer (已认证)