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]
.
解答:K 如果大于length,对 k 求余
public class Solution {
public int[] rotate(int[] nums, int k) {
if(nums == null){
return nums;
}
k = k % nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k -1);
reverse(nums, k, nums.length - 1);
return nums;
} public void reverse(int[] nums, int start, int end){
while(start < end){
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
} }
}
189. Rotate Array的更多相关文章
- 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 OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- JavaScript:this是什么?
JavaScript:this是什么?定义:this是包含它的函数作为方法被调用时所属的对象.说明:这句话有点咬嘴,但一个多余的字也没有,定义非常准确,我们可以分3部分来理解它! 1.包含它的函数.2 ...
- placeholder修改字体和颜色
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- 【Qt学习笔记】窗口部件整理
关于Qt中窗口部件的学习 今天开始学习Qt的窗口部件,领略一下Qt的神奇之处,记得2012年的那年冬天,我还学Java呢,现在基本上和Java说再见了,不过对于嵌入式的开发Qt还是举足轻重的,我想趁着 ...
- PHPStorm 与 XDebug 配置
XDebug 配置 环境 Nginx 1.4.7 32 bit PHP 5.4.25 32 bit Windows 10 64 bit 下载 PHP 5.4 VC9 (32 bit)[nts版本] 配 ...
- hive1.2.1实战操作电影大数据!
我采用的是网上的电影大数据,共有3个文件,movies.dat.user.dat.ratings.dat.分别有3000/6000和1百万数据,正好做实验. 下面先介绍数据结构: RATINGS FI ...
- JavaACOFramework的各个类介绍(part3 : Ant4ACS类)
package aco.ant; import java.util.ArrayList; import sys.Settings; import util.PseudoRandom; import a ...
- 读《编写可维护的JavaScript》第二三章总结
第二章 注释 添加注释的一般原则是,在需要让代码变得清晰时添加注释. 2.1 ① 单行注释 独占一行的注释,用来解释下一行代码.这行注释之前总是有一个空行,且缩进层级和下一行代码保持一致. 在代码行的 ...
- Javascript 数组常用操作方法
一.数组 Array 1.创建数组 /* 构造函数 */ var arr1 = new Array(); //创建一个空数组 var arr1 = new Array(5); //创建指定长度数组(数 ...
- SQL基础--索引
索引的概念: 在数据库中索引是用于提升数据库查询操作性能的一种手段,但在频繁更新的表上,索引反而会降低性能. 常用的索引结构: B*树索引 位图索引 B树索引: B书索引是最基本的索引结构,Oracl ...
- centos 7 编译zabbix 3.2.2
已有LNMP环境. 1.安装zabbix需要的依赖包,红色部门的包官方yum源没有,需要自己下载 yum install net-snmp fping iksemel net-snmp-devel ...