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

解题思路:

JAVA实现如下:

    public void rotate(int[] nums, int k) {
k%=nums.length;
k=nums.length-k;
int[] nums2=new int[k];
for(int i=0;i<k;i++)
nums2[i]=nums[i];
for(int i=0;i<nums.length-k;i++)
nums[i]=nums[i+k];
for(int i=0;i<k;i++)
nums[nums.length-k+i]=nums2[i];
}

Java for LeetCode 189 Rotate Array的更多相关文章

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

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

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

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

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

  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. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  7. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  8. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  9. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

随机推荐

  1. python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)

    ### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2)   #(上限,下限,步长)  可以实现对元素或者下标的 ...

  2. Linux文件目录权限浅谈

    1.基本权限三种(1)r (read) 读 针对目录,有读(r)权限就代表能对此目录有列表功能,就是可以执行ls命令进行查看,另外还有cp的功能.针对文件,有读(r)权限就代表能对此文件有阅读功能,可 ...

  3. linux中软链接和硬链接的区别与小结

    ln命令 该命令在文件之间创建链接.这种操作实际上是给系统中已有的某个文件指定另外一个可用于访问它的名称.对于这个新的文件名,我们可以为之指定不同的访问权限,以控制对信息的共享和安全性的问题. 如果链 ...

  4. 修改eclipse/MyEclipse中包的显示结构为树形

    在右上边三角那里进去设置 选第一个是显示完整的包名,第二个显示的是树形结构,我们一般用第一种,如下图:

  5. spring 注解简单使用

    一.通用注解 1.项目结构: 2.新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取 ...

  6. Python socket编程之二:【struct.pack】&【struct.unpack】

    import struct """通过 socket 的 send 和 recv 只能传输 str 格式的数据""" "" ...

  7. title及alt提示特效

    <html> <head> <title>title及alt提示特效</title> <style type="text/css&quo ...

  8. Linux系统管理远程登录工具PUTTY

    PuTTY 简介        PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连线软件.较早的版本仅支持Windows平台,在最近的版本中开始支持各类Unix平台,并打算移植 ...

  9. 史上最浅显易懂的Git分布式版本控制系统教程

    从零起步的Git教程,让你无痛苦上手世界上最流行的分布式版本控制系统Git! 既然号称史上最浅显易懂的Git教程,那这个教程有什么让你怦然心动的特点呢? 首先,本教程绝对面向初学者,没有接触过版本控制 ...

  10. [转] Android PhoneGap Cordova 体系结构

    说明: 本文档只针对Cordova(PhoneGap)的Android端,基于Cordova2.1.0版本. 一.总体结构 Cordova的目标是用HTML,JS,来完成手机客户端的开发,并且是只开发 ...