[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

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.

[show hint]

Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

我想到的就是反转,但是开始写的反转用了惯性思维,用下标控制,很不理想,花费大量时间还弄不精确,后来看了个湄公河,嘿嘿,一下反应到用m,n分别是起始位置和结束位置,然后一直到m

public void rotate(int[] nums, int k) {
        k = k % nums.length;
        if (k >= 1 && nums.length > k) {
            reverseArr1(nums, 0, nums.length - 1);
            reverseArr1(nums, 0, k - 1);
            reverseArr1(nums, k, nums.length - 1);
        }
    }
/**
     * 
     * @param nums
     * @param m
     *            要交换的起始位置,按照数组下标0开始
     * @param n
     *            要交换的结束位置,按照数组下标0开始
     */
    public void reverseArr1(int[] nums, int m, int n) {
        int temp = 0;
        while (m < n) {
            temp = nums[m];
            nums[m] = nums[n];
            nums[n] = temp;
            m++;
            n--;
        }
    }

这也应该算是最佳解了。给大家看看最开始写的逆转算法。蠢死了

/**
     * @param nums
     * @param m
     *            起始位置1开始
     * @param n
     *            结束位置
     */
    public void reverseArr(int[] nums, int m, int n) {
        int temp = 0, len = n - 1;
        for (int i = m - 1; i < ((n - m) / 2 + m); i++) {
            temp = nums[i];
            nums[i] = nums[len - i];
            nums[len - i] = temp;
        }
    }

还有四个解法,明天早上更新吧。

给个链接LeetCode官方谈论区

目录
相关文章
|
6月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
21 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 个子数组各自和的最大值最小。
108 0
LeetCode 410. Split Array Largest Sum
|
1天前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
18 3
|
1天前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
1天前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。