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 = ...
随机推荐
- centos在yum install报错:Another app is currently holding the yum lock解决方法
centos在yum install报错:Another app is currently holding the yum lock,这个问题可能是很多的新手经常遇到问题,之前也有人问我,包括本人在刚 ...
- MariaDB Galera Cluster 部署(如何快速部署MariaDB集群)
MariaDB Galera Cluster 部署(如何快速部署MariaDB集群) [日期:--] 来源:Linux社区 作者:Linux [字体:大 中 小] MariaDB作为Mysql的一个分 ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
- POJ1004Financial Management
这个题犯了一个小小的错误,double输出的时候用f才对,输入用lf即可.... http://poj.org/problem?id=1004 #include<stdio.h> int ...
- 使用 C# 对文件进行压缩和解压
C#中对文件压缩和可以使用两个类: GZipStream 类 此实例分为几个模块,分别为: 压缩函数: /// <summary> /// 压缩文件 /// </summary> ...
- C++和pascal之间的通信
// MyFirst.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "Winsock2.h"#pragm ...
- SpringMVC学习总结(四)——基于注解的SpringMVC简单介绍
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request ...
- Python中的SET集合操作
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- 利用python 获取 windows 组策略
工作中有时候会有这种需求: 1. 自动配置组策略的安全基线,这个东西不用你自己写了,微软有这个工具,Microsoft Security Compliance Manager,你可以在下面的地址去下载 ...
- Windows下Sublime Text 默认打开方式问题解决办法
注册表的解决办法: 删除 HKEY_CURRENT_USER\Software\Classes\Applications下的Sublime_Text.exe项.你就发现可以设置为默认打开方式了