[算法]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 = ...
随机推荐
- asp.net core mvc视频A:笔记3-5.视图数据共享之TempData
前几节讲的都是单页面数据共享,从本节开始讲跨页面数据共享 创建项目3.5,新建控制器 代码 控制器 设置TempData 另一个视图中读取TempData数据 运行 此时如果刷新页面,页面中的内容“张 ...
- 【hadoop之翊】——windows 7使用eclipse下hadoop应用开发环境搭建
由于一些缘故,这节内容到如今才写.事实上弄hadoop有一段时间了,能够编写一些小程序了,今天来还是来说说环境的搭建.... 说明一下:这篇文章的步骤是接上一篇的hadoop文章的:http://bl ...
- Amzaon EC2虚拟化技术演进:从 Xen 到 Nitro
今年2月,由光环新网运营的 AWS 中国(北京)区域和由西云数据运营的 AWS 中国 (宁夏)区域发布新的实例类型,新的实例类型包括 C5.C5d.R5.R5d.除了这四种之外,在AWS国外部分区 ...
- redis php 执行命令时,单引号和双引号的区别。
#今天遇到一个坑爹的问题,写成单引号就不行,被原样输出了. /** *判断key是否存在 */ function exists_key($key){ return $this->cmd('EXI ...
- unity 切换场景
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- python入门课程 第二章 安装Python
2-1 选择python版本首先python2.7和python3是不可以通用的目前丰富的类库都支持python2.7,所以选用Python2.7 选择python2.7版本2-2 window ...
- C语言中的编译时分配内存
1.栈区(stack) --编译器自动分配释放,主要存放函数的参数值,局部变量值等: 2.堆区(heap) --由程序员分配释放: 3.全局区或静态区 --存放全局变量和静态变量:程序结束时由系统释放 ...
- centOS6.2 最小安装下的无线网络配置
一.安装wireless_tools,http://www.linuxfromscratch.org/blfs/view/svn/basicnet/wireless_tools.html 二.vi / ...
- GS与MS之间通信
GS与MS之间通信 注意GS与MS是两个线程,现在是每个map一个线程,他们之间是内部协议进行通信的,那既然是两个线程那如何通信呢,看了net进程通信这个就比较简单了 举个例子 m_pMap-> ...
- EasyNVR无插件直播服务器播放页面的集成----单独的播放器样式
背景需求: EasyNVR自身拥有独立的客户端体系,安卓和IOS拥有各自独立的APP, 安卓下载地址:https://fir.im/EasyNVR: IOS下载可直接在APPstore搜索EasyNV ...