`

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

将一个数组右移k步,如果我们直接移动k次,会超时。我们可以先反转后k个元素,然后在反转前length - k个元素,最后将整个数组反转,就得到了结果。k在运算的时候要先于数组取模,因为k可能大于数组的长度。代码如下:
public class Solution {
    public void rotate(int[] nums, int k) {
        if(nums == null) return;
        k %= nums.length;
        reverse(nums, 0, nums.length - k);
        reverse(nums, nums.length - k, nums.length);
        reverse(nums, 0, nums.length);
    }
    public void reverse(int[] nums, int start, int end) {
        while(start < end) {
            int tem = nums[start];
            nums[start++] = nums[--end];
            nums[end] = tem;
        }
    }
    
}

分享到:
评论

相关推荐

    Coding Interview In Java

    1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth ...

    cpp-算法精粹

    Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self Game of Life Increasing Triplet Subsequence 单链表 Reverse Linked List Odd Even Linked List ...

    array-rotate:将数组旋转 n 个位置

    $ npm install --save array-rotate 用法 var rotateArray = require ( 'array-rotate' ) ; var arr = rotateArray . createArray ( 1 , 2 , 3 , 4 , 5 , 6 ) ; var positionToRotate = 2 ; var newArr = ...

    leetcode答案-LeetCode:LeetCode刷题记录,README显示实时进度/题目

    189.rotate-array.js /* * @lc app=leetcode id=189 lang=javascript * * [189] Rotate Array * * https://leetcode.com/problems/rotate-array/description/ * * algorithms * Easy (28.74%) * Total Accepted: 262...

    LeetCode最全代码

    ## Array | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 15 | [3 ...

    Algorithm-Concepts:此存储库包含计算机科学领域中使用的一些核心算法和数据结构的代码

    算法概念和算法课程。4.1 数组。... Algorithm to rotate array of size 'n' by 'd' elements6. Algorithm to segregate 0's and 1's in an array7. Find the maximum difference between two elemen

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum ...Rotate Array #0217 - Contains Duplicate #0242 - Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -

    扔鸡蛋leetcode-LeetCode-Note-Mary:玛丽的leetcode笔记本

    Rotate Array (旋转数组) #数组 33. Search in Rotated Sorted Array(搜索旋转排序数组)#数组 2020/12/08 19. Remove Nth Node From End of List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted...

    C语言 strcpy和memcpy区别详细介绍

    PS:初学算法,开始刷leetcode,Rotate array的预备知识(写的代码Time Limit Exceed难过)于是百度高效算法,本篇作为预备知识。 1、strcpy和strncpy函数 这个不陌生,大一学C语言讲过,其一般形式为strcpy(字符...

    leetcode添加元素使和等于-LeetCodeNotes:力码笔记

    Rotate Array 描述:数组向右旋转k个位置 思路:见编程珠玑2.3节 414. Third Maximum Number 描述:找出int[] A里第三大的元素,如不存在返回最大元素 思路:不用排序不用去重的解法,维护三个变量x1, x2, x3,遍

    arrayrotate:面试题

    ============ 面试问题:编写一个函数,该函数接受一个整数数组并返回该数组旋转了 N 个位置。 例如,如果 N=2,给定输入数组 [1, 2, 3, 4, 5, 6] 函数应该返回 [5, 6, 1, 2, 3, 4] 解决方案: 用递归解决。...

    impact-plugin-array-utilities:Impact JS 数组实用程序

    Impact JS 数组实用程序插件 此插件使用不可枚举的实用程序方法扩展了本机 Array 对象 安装 作为子模块,从 git 命令行: git submodule add ...Array.rotate(n) 返回数组的副本,顺

    戳气球leetcode-leetcode:leetcode

    189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array ...

    leetcode中国-DP:DP

    leetcode中国大批 0. Count Prime 1. Reverse the array 2. Find ...array ...array ...array ...array ...rotate an array by one. 8. find Largest sum contiguous Subarray [V. IMP] 9* Minimise the maximum

    leetcode中国-Final450_Data-Structures:Final450_数据结构

    leetcode中国Final450_数据结构 实际上,这个存储库包含成为优秀竞争程序员最重要的数据结构和算法。 他们的名字是: ...rotate an array by one. *find Largest sum contiguous Subarray [V. IMP] *Minimi

    array-rotator:将整数数组的元素向右移动 N 个位置

    使用 ant 构建文件来编译和运行这... 如果还想多玩玩,那就打开ArrayRotator.java类,把“array”变量(第52行)的值修改成你想要的任意值,然后用position的值修改对rotate方法的调用(第 58 行)您想将数组向右移动。

    css-mat4:从平移、旋转、缩放、倾斜制作 mat4

    rotate : [ 0 , Math . PI / 2 , - Math . PI ] , scale : [ 0.25 , 0.4 , 1.0 ] , skewX : Math . PI / 2 , skewY : Math . PI / 2 } ) //now you can do something with the 16-float matrix array... 组件...

    WPF 图片3D切换 源码

    public Rotate3DContainer() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Container_Loaded); } private void Container_Loaded(object sender, RoutedEventArgs e) { Init(); } ...

    leetcode跳跃-leetcode:leetcode解题之路

    旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...

    leetcode1004-leetcode:leetcode

    Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates from Sorted Array (E) 27. ...

Global site tag (gtag.js) - Google Analytics