[算法]Rotate Array
You may have been using Java for a while. Do you think a simple Java array question can be a challenge? Let's use the following problem to test.
Problem: 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]. How many different ways do you know to solve this problem?
Solution 1 - Intermediate Array
In a straightforward way, we can create a new array and then copy elements to the new array. Then change the original array by using System.arraycopy()
.
public void rotate(int[] nums, int k) {if(k > nums.length)k=k%nums.length;int[] result = new int[nums.length];for(int i=0; i < k; i++){result[i] = nums[nums.length-k+i];}int j=0;for(int i=k; i<nums.length; i++){result[i] = nums[j];j++;}System.arraycopy( result, 0, nums, 0, nums.length );}
Space is O(n) and time is O(n). You can check out the difference between System.arraycopy() and Arrays.copyOf().
Solution 2 - Bubble Rotate
Can we do this in O(1) space?
This solution is like a bubble sort.
public static void rotate(int[] arr, int order) {if (arr == null || order < 0) {throw new IllegalArgumentException("Illegal argument!");}for (int i = 0; i < order; i++) {for (int j = arr.length - 1; j > 0; j--) {int temp = arr[j];arr[j] = arr[j - 1];arr[j - 1] = temp;}}}
However, the time is O(n*k).
Solution 3 – Reversal
Can we do this in O(1) space and in O(n) time? The following solution does.
Assuming we are given {1,2,3,4,5,6} and order 2. The basic idea is:
1. Divide the array two parts: 1,2,3,4 and 5, 62. Rotate first part: 4,3,2,1,5,63. Rotate second part: 4,3,2,1,6,54. Rotate the whole array: 5,6,1,2,3,4
public static void rotate(int[] arr, int order) {order = order % arr.length;if (arr == null || order < 0) {throw new IllegalArgumentException("Illegal argument!");}//length of first partint a = arr.length - order;reverse(arr, 0, a-1);reverse(arr, a, arr.length-1);reverse(arr, 0, arr.length-1);}public static void reverse(int[] arr, int left, int right){if(arr == null || arr.length == 1)return;while(left < right){int temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;}}
[算法]Rotate Array的更多相关文章
- 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 ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- 理解JavaScript中的参数传递 - leetcode189. Rotate Array
1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...
- Leetcode-189 Rotate Array
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 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 ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 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 = ...
随机推荐
- FormatFloat
http://www.delphibasics.co.uk/RTL.asp?Name=FormatFloat 1 function FormatFloat ( const Formatting : ...
- 40: Redraiment的走法(不连续最长子字符串)
题目描述 : Redraiment是走梅花桩的高手.Redraiment总是起点不限,从前到后,往高的桩子走,但走的步数最多,不知道为什么?你能替Redraiment研究他最多走的步数吗? 样例输 ...
- list集合转换成json类型
public String gettext(HttpServletRequest request,HttpServletResponse response){ List<xuanhuan_> ...
- CDN与缓存的归纳理解
缓存是什么 缓存是一个到处都存在的用空间换时间的例子.通过使用多余的空间,我们能够获取更快的速度. 我们通常意义上说的缓存主要包含两部分.第一个是用户浏览器端的缓存,第二个是服务器端为了提高访问速度而 ...
- Java银行调度系统
具体需求如下: 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费.电话费之类业务的客户). 异步随机生成各种类型的客户,生成各类型用户的概率比例为: VIP客户:普通客户:快速客户 ...
- gulp配置,实例演示
项目完成后的目录 我们所需要的插件为:gulp-minify-css gulp-concat gulp-uglify gulp-rename del 如下图所示,完成后的项目目录结构: 附加,获取pa ...
- linux kernel学习笔记-5内存管理_转
void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...
- Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'createtime' at row 1...
之前项目一直好好的,之后电脑重装系统,数据库重新安装了一个5.6版本的,项目jar包丢失了,之后就又重新找了一些jar包倒入,结果运行报错: Caused by: com.mysql.jdbc.Mys ...
- 07 nginx Location之正则匹配
一:Location之正则匹配 再来看,正则也来参与. location / { root /usr/local/nginx/html; index index.html index.htm; ...
- 嵌入式专题: S5PV210 - MPEG4编码
我想说不台的平台,如tiny210和x210.它们的头文件是有稍微区别的. 我这个是x210下的代码.但都须要注意的是NV12T与NV12的问题,默认要求输入的图片是NV12T,经过调整之后,能够同意 ...