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 ...
随机推荐
- css 水平垂直居中
主要是垂直居中有点麻烦,以下代码可以实现,先记下来: <style type="text/css"> div{ border:1px solid #ccc; heigh ...
- YTU 3026: 中序线索化二叉树
原文链接:https://www.dreamwings.cn/ytu3026/2896.html 3026: 中序线索化二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 9 解决: ...
- Android存储
Android的四种数据存储方式: 1.SharedPrefrences 2.SQLite 3.Content Provider 4.File SharedPrefrences: 1.是一种轻型的数据 ...
- 向STM32 CUBE MX 生成的工程里移植stemwin
我参考这个文章做的: http://bbs.armfly.com/read.php?tid=1678 这次添加的是没有os的版本 另外跟用不用hal库没关系 1. keil自带了emwin 2. 用c ...
- servlet上传下载(任何格式的都可以)
jar不能低于此版本,JDK1.6以上,否则户报错 <dependency> <groupId>commons-fileupload</groupId> <a ...
- hive的使用02
1.hive的交互方式 1.1 bin/hive 进入hive交互命令行环境 1.2 bin/hive -e 'select * from hive.student;' (可以通过 > 将结果写 ...
- jquery中focus()函数实现当对象获得焦点后自动把光标移到内容最后
代码如下: setFocus=function(id){ var t=$("#"+id).val(); $("#"+id).val(""). ...
- JavaScript对象的chapterIII
二.DOM对象: DOM (document object model) 文档对象模型,它定义了操作文档对象的接口. DOM 把一份html文档表示为一棵家谱树,使用parent(父), child( ...
- ElasticSearch5中文分词(IK)
ElasticSearch安装 官网:https://www.elastic.co 1.ElasticSearch安装 1.1.下载安装公共密钥 rpm --import https://artifa ...
- 微信小程序-表单
wxml <view> 按钮: <button size="{{buttom.size}}" type="{{buttom.type}}" p ...