题目描述:

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.

解题思路:

牺牲空间复杂度的话可以考虑开辟新的数组空间,然后依次移动即可;

本方法只用一个临时变量存放临时数据,然后依次的交换数据,如图所示:

但是注意会陷入局部循环的局面,这时将相应的下标后移一位。

public class Solution {
public void rotate(int[] nums, int k) {
int length = nums.length;
if(length == 0)
return;
k = k % length;
int head = 0, curIndex = 0, nextIndex;
int curValue = nums[0], nextValue;
for(int i = 0; i < length; i++){
if(curIndex == head && i > 0){ //finish a circle,move to another circle
curIndex++;
head++;
curValue = nums[curIndex];
}
nextIndex = (curIndex + k) % length;
nextValue = nums[nextIndex];
nums[nextIndex] = curValue;
curIndex = nextIndex;
curValue = nextValue;
}
}
}

  

代码如下:

Java [Leetcode 189]Rotate Array的更多相关文章

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

  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. 【单调栈】Bzoj 1012: 最大数maxnumber

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 6255  Solved: 2676[Submi ...

  2. 【BZOJ】【2768】【JLOI2010】冠军调查

    网络流/最小割 我不会告诉你这题跟 BZOJ 1934 是一模一样的……包括数据范围…… /****************************************************** ...

  3. 【BZOJ】【2693】JZPTAB

    莫比乌斯反演 PoPoQQQ讲义第5题,是BZOJ 2154的升级版(多次询问) 题解:http://blog.csdn.net/popoqqq/article/details/42078725 WA ...

  4. epoll 知识总结

    poll/select/epoll 对比 http://www.cnblogs.com/apprentice89/p/3234677.html    ---有待继续学习 http://blog.chi ...

  5. 使用mysql触发器脚本,解决流水数据的添加。

    1.建立表脚本 CREATE DATABASE `spring` DEFAULT CHARACTER SET utf8; USE `spring`; CREATE TABLE `account` ( ...

  6. linux源代码阅读笔记 高速缓冲区管理

    高速缓冲区是文件系统访问块设备中数据的必经要道,为了访问文件系统等块设备上的数据,内核可以每次都访问块设备,进行读写操作. 为了提高系统性能,内核在内存中开辟一个高速数据缓冲区.在Linux内核中,高 ...

  7. Searching a 2D Sorted Matrix Part I

    Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...

  8. iframe标签用法详解(属性、透明、自适应高度)(总结)

    <iframe src="http://www.jb51.net" width="200" height="500"> 脚本之家 ...

  9. Java注解全面解析

    1.基本语法 注解定义看起来很像接口的定义.事实上,与其他任何接口一样,注解也将会编译成class文件. @Target(ElementType.Method) @Retention(Retentio ...

  10. [转]Openstack neutron 防火墙

    全文阅读传送门:http://www.ustack.com/wp-content/uploads/2013/11/Neutron%E9%98%B2%E7%81%AB%E5%A2%99.pdf 原作者: ...