[LeetCode] Rotate Array

简介: Rotate an array of n elements to the right by k steps.For example, with n=7n = 7 and k=3k = 3, the array [1,2,3,4,5,6,7][1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4][5,6,7,1,2,3,4].Note

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;
        }
    }
};
目录
相关文章
|
7月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
24 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
|
人工智能 索引
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
LeetCode 189. 旋转数组 Rotate Array
LeetCode 189. 旋转数组 Rotate Array
|
算法 Python
LeetCode 410. Split Array Largest Sum
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
111 0
LeetCode 410. Split Array Largest Sum
|
18天前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
23 3
|
18天前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
18天前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。