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 [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的更多相关文章
- 理解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 ...
- 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 ...
- 【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 = ...
随机推荐
- 思科Cisco 2960系列交换机配置命令
配置密码: 2960>en :第一次密码为空 2960h#conf t :进入全局配置模式 2960(config)#hostname swa :设置交换机名 2960(config)#enab ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
- query判断值是否为空,针对前台提交数据的校验
1.<input type="hidden" id="key" name="key" value="123"> ...
- [转](一)unity4.6Ugui中文教程文档-------概要
转载请注明出处:http://blog.csdn.net/u010019717更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/forum.php?mod=guid ...
- PHP数据类型转换 (转)
PHP数据类型转换 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: •(int).(integer):转换成整形 •(float).(double).(real):转换成浮点型 •(s ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包?
Sonatype Nexus简介 登录Nexus后,点击右侧的“Repositories”,显示当前Nexus所管理的Repository: 默认情况下Nexus为我们创建了以下主要的Reposito ...
- 关于 RxJava 技术介绍
Awesome-RxJava RxJava resources Blog 给 Android 开发者的 RxJava 详解 -强烈推荐 扔物线的文章 讲解非常详细 NotRxJava懒人专用指南 -这 ...
- Centos 7环境下编译mysql 5.7
首先在编译之前,我们要了解相关mysql 5.7的编译选项,官网编译选项地址:http://dev.mysql.com/doc/refman/5.7/en/source-configuration-o ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...