LeetCode & Q189-Rotate Array-Easy
Array
Description:
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].
刷题还是习惯早上刷,现在脑子不灵光了,哎...
这题我想的很复杂,可以说和算法不沾边,看了Discuss照着写了一遍。
public class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);
}
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--;
}
}
}
LeetCode & Q189-Rotate Array-Easy的更多相关文章
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 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: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 ...
- LeetCode(67)-Rotate Array
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- 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,.. ...
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
随机推荐
- 应用canvas绘制动态时钟--每秒自动动态更新时间
使用canvas绘制时钟 下文是部分代码,完整代码参照:https://github.com/lemoncool/canvas-clock,可直接下载. 首先看一下效果图:每隔一秒会动态更新时间 一. ...
- SpringMVC常用注解整理
一.组件型注解: @Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean. @Repository 对Dao实现类进行注解 (特殊的@Compone ...
- oracle使用中的一些问题
一.设置自增主键(假设表的主键名为:company_id) 1)创建序列(company_autoinc): maxvalue start increment nocache; 2)创建触发器(com ...
- conda创建py27虚拟环境安装theano(anaconda3)
现在python3已经成为主流的python环境,大部分的package都兼容python3,仍然有一小部分,或者说是某一领域的package需要使用python2.本人现在主要在利用python做机 ...
- python 批量删除mysql前缀相同的表
1,一般游戏log数据库会存储大量的玩家行为日志,一种行为一张表,每天生成一张新表,一天会有30+张不同行为的表,通常会保留玩家日志1年左右,对于超过1年的日志需要删除 2,log数据库一年会保存1W ...
- "码率适配限速”,如何使带宽成本减少30%?
3月28日.29日,B站.爱奇艺即将先后完成IPO.爱奇艺的招股书显示,爱奇艺依然处于亏损状态.2015 年.2016 年.2017 年三年合计亏损约 94 亿元.高昂的版权费是造成视频网站亏损的重要 ...
- Spring Security 源码解析(一)AbstractAuthenticationProcessingFilter
# 前言 最近在做 Spring OAuth2 登录,并在登录之后保存 Cookies.具体而言就是 Spring OAuth2 和 Spring Security 集成.Google一下竟然没有发现 ...
- Python基础-week01
本节内容摘要:http://www.cnblogs.com/Jame-mei Python介绍 Python是怎么样的语言? Python 2 or 3? 安装 Hello World程序 变量 用户 ...
- Elasticsearch就这么简单
一.前言 最近有点想弄一个站内搜索的功能,之前学过了Lucene,后来又听过Solr这个名词.接着在了解全文搜索的时候就发现了Elasticsearch这个,他也是以Lucene为基础的. 我去搜了几 ...
- FastJson简单使用
首先建立两个实体类,Student.java 和 Teacher.java public class Student { private int id; private String name; pr ...