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 = ...
随机推荐
- TCP/IP协议(二)
2014-09-04 11:03:27 注:关于seq 和 ack 的理解,seq为发送的字节的第一个序号,一直累加,ack接收字节的最后一个序号+1,建立连接和结束连接时的SYN.FIN标志位占 ...
- EasyUI datebox 只读和取值
<input id="dd" type="text" class="easyui-datebox" required="re ...
- 【Tomcat】Tomcat Session在Redis共享
参考的优秀文章 Redis-backed non-sticky session store for Apache Tomcat 简单地配置Tomcat Session在Redis共享 我使用的是现有的 ...
- c++ ado 调用存储过程并得到输出参数和返回值
// AccessSqlserverByAdo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h ...
- O(1) Check Power of 2 - LintCode
examination questions Using O(1) time to check whether an integer n is a power of 2. Example For n=4 ...
- Struts2拦截器初涉
Struts2拦截器初涉 正在练习struts,本例是从一个pdf上摘抄的例子,那本pdf都不知道叫什么名字,不过感觉很适合初学者. 在这里要实现一个简单的拦截器"GreetingInter ...
- <hr/> 水平线样式
<hr style="width:490px;" /> <hr size=8 style="COLOR: #ffd306;border-style:ou ...
- ssh框架介绍
SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...
- winform小程序---猜拳小游戏
因为学的时间不长,所以借鉴了一些资料做了这个小程序,大家共同学习,共同进步.感觉很有自信,世上无难事,只怕有心人. using System; using System.Collections.Gen ...
- 配置spring事务管理的几种方式(声明式事务)
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...