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

    一.简介 Emmet (前身为 Zen Coding) ,不是软件也不是代码,是编辑器(如sublime text)的插件,相应的后缀文件(.html/.css)输入指定的缩写语法,按下tab键就能生 ...

  2. jeecms内容显示条数

    1.按照1.2.3.4.5顺序显示 <div class="index-news"> [@cms_channel id='1'] <h2><span& ...

  3. Put-Me-Down项目Postmortem

    设想和目标 PMD是一款帮助低头族控制使用手机时间的APP,设想按照需求规格说明书内容实现功能,能将数据备份到服务器. 计划 初始计划我们是想将程序方面分为安卓和后台,主要是程序方面的工作.我们对项目 ...

  4. IOS基础之 (四) OC对象

    一 建立一个OC的类 完整的写一个函数:需要函数的声明和定义. 完整的写一个类:需要类的声明和实现. 1.类的声明 声明对象的属性和行为 #import <Foundation/Foundati ...

  5. POJ2676Sudoku(类似于八皇后)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16444   Accepted: 8035   Special ...

  6. [Angularjs]视图和路由(二)

    写在前面 上篇文章主要介绍了视图和路由的基本概念,并在文章最后举了一个简单的使用案例.这篇文章将继续学习路由的配置,及相关参数的说明. 系列文章 [Angularjs]ng-select和ng-opt ...

  7. Javascript输出表格

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Visual Studio Online Integrations-Collaboration

                                                                                         原文:http://www.v ...

  9. Linux 下的另一个密码破解工具medusa

    首先,本人在此声明 此工具许合理利用非法破解很可能会被发现的因为这是一个暴力破解方式需要不断的尝试登陆服务器 ,服务器上的检测软件很快可以跟踪到并锁定你的IP地址 请大家切勿用于非法手段- -! me ...

  10. icon@font-face那些事

    http://files.cnblogs.com/files/LoveOrHate/font.rar @font-face { font-family: 'Glyphicons Regular'; s ...