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

    TTL(Time To Live )是IP协议包中的一个值,它告诉网络,数据包在网络中的时间是否太长而应被丢弃.有很多原因使包在一定时间内不能被传递到目的地.解决方法就是在一段时 间后丢弃这个包,然后 ...

  2. 实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty 和Staff。

    (1)Person类中的属性有:姓名name(String类型),地址address(String类型), 电话号码telphone(String类型)和电子邮件地址email(String类型): ...

  3. 图片上传本地预览。兼容IE7+

    基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari 预览地址:http://www.jinbanmen.com/test/1.html js代码:/**名称 ...

  4. 有向图的强连通分量——Tarjan

    在同一个DFS树中分离不同的强连通分量SCC; 考虑一个强连通分量C,设第一个被发现的点是 x,希望在 x 访问完时立刻输出 C,这样就可以实现 在同一个DFS树中分离不同的强连通分量了. 问题就转换 ...

  5. WPFの三种方式实现快捷键

    最近,对wpf添加快捷键的方式进行了整理.主要用到的三种方式如下: 一.wpf命令: 资源中添加命令 <Window.Resources> <RoutedUICommand x:Ke ...

  6. python 学习笔记九 队列,异步IO

    queue (队列) 队列是为线程安全使用的. 1.先入先出 import queue #测试定义类传入队列 class Foo(object): def __init__(self,n): self ...

  7. C++11 图说VS2013下的引用叠加规则和模板参数类型推导规则

    背景:    最近在学习C++STL,出于偶然,在C++Reference上看到了vector下的emplace_back函数,不想由此引发了一系列的“探索”,于是就有了现在这篇博文. 前言:     ...

  8. Java 中的 static 使用之静态变量

    大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 static 大显身手的时候了!! Java 中 ...

  9. js插入动态脚本

    原文章:https://www.w3cmm.com/dom/insert-javascript.html 动态脚本指的是在页面加载时不存在,但将来的某一时刻通过修改该DOM动态添加的脚本.和操作HTM ...

  10. Hibernate <二级缓存>

    二级缓存: 定义: 1.二级缓存被称为进程级缓存或者sessionFactory级缓存,二级缓存可以被所有session共享 2.二级缓存的生命周期和sessionFactory生命周期一样(sess ...