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.


拿到题目第一印象就是每次右移一个位置,按次数k进行k次循环。

代码一:

 public class test1 {
public static void rotate(int[] nums, int k) {
for (int i = 0; i < k; i++)
rotateOneStep(nums);
} public static void rotateOneStep(int[] nums) {
int tmp = nums[nums.length - 1];
for (int i = nums.length - 1; i > 0; i--) {
nums[i] = nums[i - 1];
}
nums[0] = tmp;
} public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 };
rotate(array, 3);
for(int i = 0; i < array.length;i++)
System.out.println(array[i]);
}
}

测试了下运行OK,但问题是效率不高,复杂度为O(k*n),提交上去显示Time Limit Exceeded。但是实现了空间复杂度是O(1)。

第二种方法就是扩容,在原数组后面复制一遍,之后截取。这里一个问题就是会额外占用许多空间。(另一个类似的方法是直接从第k个节点复制给新建数组)算法复杂度都为O(n)

代码二:

 public class test1 {
public static void rotate(int[] nums, int k) {
k = k % nums.length;
int[] temp = new int[nums.length * 2];
for (int i = 0; i < nums.length; i++) {
temp[i] = nums[i];
temp[i + nums.length] = nums[i];
}
int res = nums.length - k;
for (int i = 0; i < nums.length; i++) {
nums[i] = temp[res];
res++;
}
} public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 };
rotate(array, 3);
for(int i = 0; i < array.length; i++ )
System.out.println(array[i]);
}
}

代码三:

 public class test1 {
public static void rotate(int[] nums, int k) {
int[] copyNums = new int[nums.length];
for( int i = 0 ; i < nums.length ; i++ )
copyNums[( i + k ) % nums.length] = nums[i];
for(int i = 0; i < copyNums.length; i++ ){
nums[i] = copyNums[i];
System.out.println(nums[i]);
}
} public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 ,8};
rotate(array, 3);
}
}

第三种思路更清晰,效率更高,跟字符串自由旋转一样的操作,采用三次翻转法。第一次翻转前n-k个,第二次翻转后k个,第三次翻转全部。该方法在n足够大时,效率最好。时间复杂度O(n),空间O(1)(需要判定k的值)

代码四:

 public class test1 {
public static void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - k - 1);
reverse(nums, nums.length - k, nums.length - 1);
reverse(nums, 0, nums.length - 1);
} private static int[] reverse(int[] array, int begin, int end) {
int temp;
for (; begin < end; begin++, end--) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
}
return array;
} public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
rotate(array, 3);
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
}
}

2015-04-16

LeetCode189——Rotate Array的更多相关文章

  1. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  2. Leetcode-189 Rotate Array

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

  3. LeetCode189: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. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  5. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  6. 【LeetCode】Rotate Array

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

  7. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  8. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  9. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. “stdafx.h”: No such file or directory

    “stdafx.h”: No such file or directory 一般原因是建工程的时候选择了空工程,然后添加现有源文件(含stdafx.cpp) 或者 修改了已有的stdafx.cpp 或 ...

  2. python之rabbitMQ篇

    一.RabbitMQ安装 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统,它遵循Mozilla Pulic License开源协议. MQ全称为Message Queue,消息队列 ...

  3. Duilib实现QQ聊天窗口晃动

    转载:http://blog.csdn.net/arbboter/article/details/26282717 转载:http://blog.csdn.net/zerolusta/article/ ...

  4. python: 模块发布

    一.准备发布 1.为模块文件创建一个文件夹,并将模块文件复制到这个文件中(一般,文件夹的名字和模块的名字一样) 2.在文件夹中创建一个名为『setup.py』的文件,内容如下: #encoding:u ...

  5. js获取url参数 兼容某些带#url

    网上有大把现成的代码,不过有点小小的瑕疵 例如目前最流行的 正则法: function getArgument(_arg) { var reg = new RegExp("(^|&) ...

  6. ubuntu15.04 安装 pylab失败,先记下来,漫漫看

    pydo@planpls:/var/python/web2py$ pip install pylab Downloading/unpacking pylab Downloading pylab-0.1 ...

  7. 【转】Linux系统启动过程分析

    [转]Linux系统启动过程分析 转自:http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对Linux系统有了一定了解和熟悉后,想对其更 ...

  8. Mac下安装Wireshark,双击闪退

     Mac OS X上使用Wireshark抓包(http://blog.csdn.net/phunxm/article/details/38590561) Mac下安装Wireshark /Appli ...

  9. 6个html5页面适配iphone6的技巧

    iphone6及iphone6plus已经出来一段时间了.很多移动端网站,以前写死body为320px的,现在估计也忙着做适配了. 大屏幕手机其实一直有,只是以前大家没怎么重视,移动端的H5页面大部分 ...

  10. 转:C++中的单例模式

    C++中的单例模式 单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块, ...