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 ...
随机推荐
- js封装类
1.闭包函数中定义基类,使用基类定义类方法 ;var Appkit = function() { //定义类 var obj = new Object(); // 定义方法 obj.isWeixin ...
- maven插件
sql-maven-plugin: http://www.mojohaus.org/sql-maven-plugin/ 常用插件: http://www.trinea.cn/android/maven ...
- mysql 删除时候有外键提示问题解决
在直接调用delete 语句的时候,如果出现了外键错误提示的时候,可以考虑用下面的语句执行. 原理是去除外键提示,先用外键约束,再取消外键约束即可 SET FOREIGN_KEY_CHECKS=1;D ...
- http常见状态码解析
自定义 Ajax原生编写ajax:function(opt){ var xmlhttp; //创建对象 if (window.XMLHttpRequest){// code for IE7+, Fir ...
- 前端之CSS(一)
一.什么是CSS CSS是Cascading Style Sheets,层叠样式表,高大上的说法是用来控制网页数据的表现,可以使网页的表现与数据内容分离.通俗来讲,就是用各种盒子的堆叠实现我们想要的H ...
- $smarty获取变量get,post等用法
{$smarty}保留变量不需要从PHP脚本中分配,是可以在模板中直接访问的数组类型变量,通常被用于访问一些特殊的模板变量.例如,直接在模板中访问页面请求变量.获取访问模板时的时间邮戳.直接访问PHP ...
- 5分钟开启Esper之旅
原作者:CoffeeOneSugar 翻译:刘斌华 在我之前发表的文章中,我提到我最近热衷于Complex Event Processing (CEP) (复杂事件处理).简单来说,CEP把数据流作为 ...
- 高通平台FastMMI(FFBM模式)简介与进入方法
参考: http://blog.csdn.net/tfslovexizi/article/details/51499979 http://www.voidcn.com/blog/jimbo_lee/a ...
- dom节点的操作
dom节点的操作 -- jQuery 内部插入 1.(结尾)append 方法 . appendto方法(为了方便链式操作) (开头)prepend方法 $('#div1').ap ...
- cookie手工注入
1.先访问当前注入点文件名 2.修改cookie javascript:alert(document.cookie="id="+escape("1137")); ...