- 浏览: 174210 次
- 性别:
- 来自: 济南
文章分类
最新评论
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可能大于数组的长度。代码如下:
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; } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 228Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 231You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 344Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 337Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 458Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 520Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 434Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 623Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 432The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 391Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 537Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 374All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 866Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 558Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 604Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 766Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 738You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 634For a undirected graph with tre ...
相关推荐
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 ...
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 ...
$ 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 = ...
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...
## Array | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 15 | [3 ...
算法概念和算法课程。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 浇花力扣解决方案 简单的 #0001 - Two Sum ...Rotate Array #0217 - Contains Duplicate #0242 - Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -
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...
PS:初学算法,开始刷leetcode,Rotate array的预备知识(写的代码Time Limit Exceed难过)于是百度高效算法,本篇作为预备知识。 1、strcpy和strncpy函数 这个不陌生,大一学C语言讲过,其一般形式为strcpy(字符...
Rotate Array 描述:数组向右旋转k个位置 思路:见编程珠玑2.3节 414. Third Maximum Number 描述:找出int[] A里第三大的元素,如不存在返回最大元素 思路:不用排序不用去重的解法,维护三个变量x1, x2, x3,遍
============ 面试问题:编写一个函数,该函数接受一个整数数组并返回该数组旋转了 N 个位置。 例如,如果 N=2,给定输入数组 [1, 2, 3, 4, 5, 6] 函数应该返回 [5, 6, 1, 2, 3, 4] 解决方案: 用递归解决。...
Impact JS 数组实用程序插件 此插件使用不可枚举的实用程序方法扩展了本机 Array 对象 安装 作为子模块,从 git 命令行: git submodule add ...Array.rotate(n) 返回数组的副本,顺
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中国大批 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_数据结构 实际上,这个存储库包含成为优秀竞争程序员最重要的数据结构和算法。 他们的名字是: ...rotate an array by one. *find Largest sum contiguous Subarray [V. IMP] *Minimi
使用 ant 构建文件来编译和运行这... 如果还想多玩玩,那就打开ArrayRotator.java类,把“array”变量(第52行)的值修改成你想要的任意值,然后用position的值修改对rotate方法的调用(第 58 行)您想将数组向右移动。
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... 组件...
public Rotate3DContainer() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Container_Loaded); } private void Container_Loaded(object sender, RoutedEventArgs e) { Init(); } ...
旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...
Rotate Image (M) -> 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) -> 2 26. Remove Duplicates from Sorted Array (E) 27. ...