[LeetCode] Rotate Array

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.

解题思路1

首先把数组复制一遍,然后找到元素之间的映射关系: newnum[i] = oldnum[(i - k + n) % n],时间复杂度为O(n),空间复杂度为O(n)

实现代码1

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 16:58
    *  @Status   : Accepted
    *  @Runtime  : 33 ms
******************************************************************/
class Solution {
public:
    void rotate(int nums[], int n, int k) {
        int *temp = new int[n];
        memcpy(temp, nums, n * sizeof(int));

        k = k % n;
        for (int i = 0; i < n; i++)
        {
            nums[i] = temp[(i - k + n) % n];
        }

        delete [] temp;
    }
};

解题思路2

将数组看成是一个环,每个元素每次往前走一步,循环k次。时间复杂度为O(k*n),耗时较长,空间复杂度为O(1)

实现代码2

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 17:10
    *  @Status   : Accepted
    *  @Runtime  : 872 ms
******************************************************************/
class Solution {
public:
    void rotate(int nums[], int n, int k) {
        k = k % n;
        while (k--)
        {
            int temp = nums[n - 1];
            for (int i = n - 1; i > 0; i--)
            {
                nums[i] = nums[i - 1];
            }
            nums[0] = temp;
        }
    }
};

解题思路3

①将整个数组反转
②将由分割点分割的两个数组分别反转
即:1 2 3 4 5 6 7 -> 7 6 5 | 4 3 2 1 -> 5 6 7 | 1 2 3 4
时间复杂度为O(n),空间复杂度为O(1)

实现代码3

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 17:39
    *  @Status   : Accepted
    *  @Runtime  : 25 ms
******************************************************************/
class Solution {
public:
    void rotate(int nums[], int n, int k) {
        k = k % n;
        rev(nums, 0, n - 1);
        rev(nums, 0, k - 1);
        rev(nums, k, n - 1);
    }

    void rev(int num[], int left, int right)
    {
        int temp;
        while (left < right)
        {
            temp = num[left];
            num[left++] = num[right];
            num[right--] = temp;
        }
    }
};
时间: 2024-09-07 11:27:03

[LeetCode] Rotate Array的相关文章

[LeetCode]189.Rotate Array

题目 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

[LeetCode] Circular Array Loop 环形数组循环

You are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it's negative (-n), move backward n steps. Assume the first element of the array is forward next to the last el

[LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

[LeetCode] Non-decreasing Array 非递减数列

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1element. We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n). Example 1: Input: [4,2,3] Outp

【LeetCode从零单排】No189 .Rotate Array

题目 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

[LeetCode] Patching Array

Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. Example

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

Matlab编程基础

原文:Matlab编程基础   平台:Win7 64 bit,Matlab R2014a(8.3)   "Matlab"是"Matrix Laboratory" 的缩写,中文"矩阵实验室",是强大的数学工具.本文侧重于Matlab的编程语言侧面,讲述Matlab的基本语法,以及用Matlab语言进行程序设计.值得一提的是,Matlab从R2014a版本开始支持中文语言了!   1.基本概念 Matlab默认启动后界面: Matlab有关的文件后缀

《JavaScript应用程序设计》一一2.15 无状态函数(纯函数)

2.15 无状态函数(纯函数) 纯函数往往是没有状态的.这意味着它在执行时不会对外界的变量.对象.数组等值进行修改.纯函数的输入与输出具有一对一的映射关系,无论它被使用者调用多少次.下面是一个普通函数(非纯函数): var rotate = function rotate(arr) { arr.push(arr.shift()); return arr; } test('Rotate', function () { var original = [1, 2, 3]; deepEqual(rota