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 ...
随机推荐
- mysql 5.6 online ddl
innodb存储引擎实现online ddl的原理是在执行创建或删除操作的同时,将DML操作日志写入到一个缓存中,待完成索引创建后再重做应用到表上,以此达到数据的一致性,这个缓存大小由参数innodb ...
- MySQL常用语句
第1章 SQL结构化语言 1.什么是SQL? SQL,英文全称Structured Query Language,中文意思是结构化查询语言,它是一种关系型数据库中的数据进行定义和操作的语言方法,是大多 ...
- python之RabbitMQ
一.安装RabbitMQ 1. 安装erlang 1 2 3 4 tar xf otp_src_18.3.tar.gz cd otp_src_18.3 ./configure --prefix=/ma ...
- Intellij jrebel 热部署
Intellij 14破解下载 注册机 即可进行破解.JRebel安装下载IntelliJ IDEA的 JRebel插件: jr-ide-idea-6.2.0-idea-13-14.zip. 打开In ...
- js 、jsdoc生成33
============== js 点击事件后没方法名,调用有方法名 document.getElementById('lind').onclick=abc;//传统的id选择器 中没有# 哦 fun ...
- R画图中英文字体完美解决方案
在某些时候,需要在R画图中添加中文,但是默认情况下,R对中文的支持不好. 当用R画PDF图,并且图中有中文的时候,安装并加载如下包library(showtext)然后:showtext.auto(e ...
- blue and red ball
#include<iostream> #include<cstring> using namespace std; int sum; ]; int n; int head; i ...
- 使用xfire工具搭建webservice
一个简单的项目,下载下来导入可以直接使用 https://yunpan.cn/cY8ANUAYLgy7s 访问密码 99e3
- Spark学习(二) -- Spark整体框架
标签(空格分隔): Spark 还记得上次的wordCount程序嘛?通过这个小程序,我们来一窥Spark的框架是什么样子的. sc.textFile("/usr/local/Cellar/ ...
- 企业办公3D指纹考勤系统解决方案
员工准时.正常出勤是企业考勤制度的基本要求,然而目前签名式.卡钟式.IC卡考勤系统均存在代打卡.人情卡.不易统计等漏洞,而市面上的光学指纹考勤机存在识别能力差.识别速度慢.使用寿命短.不能完全杜绝指纹 ...