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


题目标签:Array

  题目给了我们一个数组 和 k。 让我们 旋转数组 k 次。

  这里有一个很巧妙的方法:

    利用数组的length - k 把数组 分为两半;

    reverse 左边和右边的数组;

    reverse 总数组。

  举一个例子:

  1 2 3 4 5 6 7  如果k = 3 的话, 会变成 5 6 7 1 2 3 4

  1 2 3 4 5 6 7  middle = 7 - 3 = 4,分为左边 4个数字,右边 3个数字

  4 3 2 1 7 6 5  分别把左右reverse 一下

  5 6 7 1 2 3 4  把总数组reverse 一下就会得到答案

Java Solution:

Runtime beats 15.37%

完成日期:04/13/2017

关键词:Array

关键点:利用k把数组分两半;reverse左右两边数组;reverse总数组

 public class Solution
{
public void rotate(int[] nums, int k)
{
if(nums == null || nums.length == 0 || k % nums.length == 0)
return; int turns = k % nums.length;
int middle = nums.length - turns; reverse(nums, 0, middle-1); // reverse left part
reverse(nums, middle, nums.length-1); // reverse right part
reverse(nums, 0, nums.length-1); // reverse whole part
} public void reverse(int[] arr, int s, int e)
{
while(s < e)
{
int temp = arr[s];
arr[s] = arr[e];
arr[e] = temp; s++;
e--;
}
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4298711.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 189. Rotate Array (旋转数组)的更多相关文章

  1. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  2. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

  3. [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  ...

  4. Rotate Array 旋转数组 JS 版本解法

    Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...

  5. Leetcode 189 Rotate Array stl

    题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...

  6. rotate array 旋转数组

    class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0 ...

  7. Java [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 ...

  8. C#解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  ...

  9. Java for 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. 将缓冲区的数字字符串转化成BCD码数据_INT PubNumericToBCDStr(_UCHR *pcNStr, _INT iNLen, _UCHR *pcBCDStr)

    INT PubNumericToBCDStr(_CHR *pcNStr, _INT iNLen, _CHR *pcBCDStr) { _UCHR *pN = pcNStr; _UCHR *pB = p ...

  2. Activiti第三篇【连接、排他网关、指定任务处理人、组任务】

    连线 上面我们已将学过了流程变量了,可以在[任务服务.运行时服务.流程开始.完成某个任务时设置流程变量],而我们的连接就是流程变量的实际应用了-. 定义流程图 我们并不是所有的流程都是按一条的路径来走 ...

  3. 《HiBlogs》重写笔记[1]--从DbContext到依赖注入再到自动注入

    本篇文章主要分析DbContext的线程内唯一,然后ASP.NET Core的注入,再到实现自动注入. DbContext为什么要线程内唯一(非线程安全) 我们在使用EF的时候,可能使用相关框架封装过 ...

  4. python基础之条件循环语句

    前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 0x00. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语 ...

  5. Java 编程思想 Chapter_14 类型信息

    本章内容绕不开一个名词:RTTI(Run-time Type Identification) 运行时期的类型识别 知乎上有人推断作者是从C++中引入这个概念的,反正也无所谓,理解并能串联本章知识才是最 ...

  6. noip的一些模板(参考了神牛的博客)

    一.图论 1.单源最短路 洛谷P3371 (1)spfa 已加SLF优化 419ms #include <iostream> #include <cstdio> #includ ...

  7. Maven在Windows中的配置以及IDE中的项目创建

    Maven在Windows下的配置 1.Maven下载地址:http://maven.apache.org/download.cgi,下载红框里的版本即可. 2.解压到D盘: 3.修改配置文件sett ...

  8. 第4章 同步控制 Synchronization ---哲学家进餐问题(The Dining Philosophers)

    哲学家进餐问题是这样子的:好几位哲学家围绕着餐桌坐,每一位哲学家要么思考,要么等待,要么就吃饭.为了吃饭,哲学家必须拿起两支筷子(分放于左右两端).不幸的是,筷子的数量和哲学家相等,所以每支筷子必须由 ...

  9. Dubbo分布式服务子系统的划分

    一.划分子系统的策略 按照系统的业务模块的独立性划分 二.划分时服务子系统的数量的控制 过多:可能划分过细,破坏业务子系统的独立性,部署维护工作量大,独立进程占用内存多 过少:没能很好的解耦,开发维护 ...

  10. redis C接口hiredis 简单函数使用介绍

    hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了. 函数原型:redisContext *redisConnect(const char ...