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. python:页面布局 后台管理页面之常用布局

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. Android study first ----------安卓项目目录结构及adb指令

    #Android项目的目录结构 * Activity:应用被打开时显示的界面 * src:项目代码 * R.java:项目中所有资源文件的资源id * Android.jar:Android的jar包 ...

  3. OpenCV安装配置的简单记录

    在ubuntu16.04下安装OpenCV 2.4.11的简单记录 1. 安装cmake,执行$apt-get install cmake即可,cmake -version验证 2. 下载OpenCV ...

  4. [问题2014S11] 解答

    [问题2014S11]  解答 我们先引用一下复旦高代书 P310 的习题 6, 其证明可参考白皮书 P257 的例 8.33: 习题6  设实二次型 \(f(x_1,x_2,\cdots,x_n)= ...

  5. Spring 定时任务2

    转载自http://www.cnblogs.com/nick-huang/p/4864737.html > 版本说明 <dependencies> <dependency> ...

  6. IE/Firefox/Chrome等浏览器保存Cookie的位置

    IE/Firefox/Chrome等浏览器保存Cookie的位置 原文  http://smilejay.com/2013/04/browser-cookie-location/   前面写了篇长文( ...

  7. CentOS----使用yum命令出现“could not retrieve mirrorlist http://mirrorlist.centos.org ***” - ybq155”

    无聊安装了个mini版的32位的CentOS 6.5,进来想安装个东西,yum install emacs 提示什么 Loaded plugins: fastestmirror, refresh-pa ...

  8. Open source packages on self-driving car

    Autoware https://github.com/CPFL/Autoware.git Open-source software for urban autonomous driving &quo ...

  9. php : 匿名函数(闭包) [二]

    摘自: http://www.cnblogs.com/yjf512/archive/2012/10/29/2744702.html php的闭包(Closure)也就是匿名函数.是PHP5.3引入的. ...

  10. 【CSU1808】地铁

    ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ...