Java [Leetcode 189]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.
解题思路:
牺牲空间复杂度的话可以考虑开辟新的数组空间,然后依次移动即可;
本方法只用一个临时变量存放临时数据,然后依次的交换数据,如图所示:
但是注意会陷入局部循环的局面,这时将相应的下标后移一位。
public class Solution {
public void rotate(int[] nums, int k) {
int length = nums.length;
if(length == 0)
return;
k = k % length;
int head = 0, curIndex = 0, nextIndex;
int curValue = nums[0], nextValue;
for(int i = 0; i < length; i++){
if(curIndex == head && i > 0){ //finish a circle,move to another circle
curIndex++;
head++;
curValue = nums[curIndex];
}
nextIndex = (curIndex + k) % length;
nextValue = nums[nextIndex];
nums[nextIndex] = curValue;
curIndex = nextIndex;
curValue = nextValue;
}
}
}
代码如下:
Java [Leetcode 189]Rotate Array的更多相关文章
- Java for LeetCode 189 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 (旋转数组)
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 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- C#解leetcode 189. 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 stl
题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with 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 = ...
随机推荐
- 【BZOJ】【1029】【JSOI2007】建筑抢修
贪心 按T2(完成时限)排序,然后从前往后依次枚举 如果sum+a[i].t1<=a[i].t2则加入 如果来不及修这个建筑: 如果当前这个建筑的维修时间t1比之前修过的建筑中耗时最长的耗时短, ...
- PAT-乙级-1050. 螺旋矩阵(25)
1050. 螺旋矩阵(25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求将给定的N个正整数按非递增的 ...
- 【WCF--初入江湖】目录
[WCF--初入江湖]目录 [WCF--初入江湖]01 WCF编程概述 [WCF--初入江湖]02 WCF契约 [WCF--初入江湖]03 配置服务 [WCF--初入江湖]04 WCF通信模式 [WC ...
- Kafka的消息格式
Commit Log Kafka储存消息的文件被它叫做log,按照Kafka文档的说法是: Each partition is an ordered, immutable sequence of me ...
- vsftp在REDHAT,CENTOS 5中登录慢的解决办法
vsftp在REDHAT,CENTOS 5中登录慢的解决办法 vsftp在REDHAT,CENTOS 5中不仅登录慢,至少花30秒左右,而且上传文件的速度也受影响, 经过摸索,根本原因在DNS解析上花 ...
- winform 开发之Control.InvokeRequired
Control.InvokeRequired 获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中. InvokeRequir ...
- HorseCome
紫气东来,祝福也随之而来,喜鹊登梅,福禄也登上眉梢,马年将至,喜庆将萦绕身旁,在这个美好的日子送上我最真挚的祝福,祝身体安康. 春晓,春晓,处处绿杨芳草.山山水水,欢欢笑笑,共祝六合同春,步步登高!
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Linux 修改计算机名
查看计算机名:在终端输入hostname 修改的话 hostname +计算机名(重启后失效) 要永久修改的话要修改配置文件/etc/sysconfig/network 修改hostname=你要改的 ...
- 各种分区类型对应的partition_Id
ID Name Note == ==== ==== 00h empty [空] 01h DOS 12-bit FAT [MS DOS FAT12] 02h XENIX root file system ...