专栏首页Reck ZhangLeetCode 0189 - Rotate Array

LeetCode 0189 - Rotate Array

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....

    Tyan
  • Leetcode 189 Rotate Array

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

    triplebee
  • leetcode-189-Rotate Array

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

    chenjx85
  • 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++,直到交换次...

    ShenduCC
  • LeetCode笔记:189. Rotate Array

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

    Cloudox
  • 挑战程序竞赛系列(11):2.5最短路径

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

    用户1147447
  • Array - 189. Rotate Array

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

    用户5705150
  • LeetCode 189:旋转数组 Rotate Array

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

    爱写bug
  • LeetCode 189:旋转数组 Rotate Array

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

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

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

    WZEARW
  • Q189 Rotate Array

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

    echobingo
  • 【LeetCode】数组--旋转数组(189)

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

    PM小王
  • Array - 48. Rotate Image

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

    用户5705150
  • LeetCode 189. 旋转数组(环形替换)

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

    Michael阿明
  • 程序员面试金典 - 面试题 10.03. 搜索旋转数组(二分查找)

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

    Michael阿明
  • C语言中的复制函数(strcpy和memcpy)第一章

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

    互联网CEO
  • Leetcode 题目列表(难度、出现频率、知识点)

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

    triplebee

扫码关注云+社区

领取腾讯云代金券