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的更多相关文章

  1. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  2. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  3. 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  ...

  4. 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 ...

  5. 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  ...

  6. 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 ...

  7. 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  ...

  8. 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 ...

  9. 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,.. ...

  10. 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 ...

随机推荐

  1. 应用canvas绘制动态时钟--每秒自动动态更新时间

    使用canvas绘制时钟 下文是部分代码,完整代码参照:https://github.com/lemoncool/canvas-clock,可直接下载. 首先看一下效果图:每隔一秒会动态更新时间 一. ...

  2. SpringMVC常用注解整理

    一.组件型注解: @Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean. @Repository 对Dao实现类进行注解 (特殊的@Compone ...

  3. oracle使用中的一些问题

    一.设置自增主键(假设表的主键名为:company_id) 1)创建序列(company_autoinc): maxvalue start increment nocache; 2)创建触发器(com ...

  4. conda创建py27虚拟环境安装theano(anaconda3)

    现在python3已经成为主流的python环境,大部分的package都兼容python3,仍然有一小部分,或者说是某一领域的package需要使用python2.本人现在主要在利用python做机 ...

  5. python 批量删除mysql前缀相同的表

    1,一般游戏log数据库会存储大量的玩家行为日志,一种行为一张表,每天生成一张新表,一天会有30+张不同行为的表,通常会保留玩家日志1年左右,对于超过1年的日志需要删除 2,log数据库一年会保存1W ...

  6. "码率适配限速”,如何使带宽成本减少30%?

    3月28日.29日,B站.爱奇艺即将先后完成IPO.爱奇艺的招股书显示,爱奇艺依然处于亏损状态.2015 年.2016 年.2017 年三年合计亏损约 94 亿元.高昂的版权费是造成视频网站亏损的重要 ...

  7. Spring Security 源码解析(一)AbstractAuthenticationProcessingFilter

    # 前言 最近在做 Spring OAuth2 登录,并在登录之后保存 Cookies.具体而言就是 Spring OAuth2 和 Spring Security 集成.Google一下竟然没有发现 ...

  8. Python基础-week01

    本节内容摘要:http://www.cnblogs.com/Jame-mei Python介绍 Python是怎么样的语言? Python 2 or 3? 安装 Hello World程序 变量 用户 ...

  9. Elasticsearch就这么简单

    一.前言 最近有点想弄一个站内搜索的功能,之前学过了Lucene,后来又听过Solr这个名词.接着在了解全文搜索的时候就发现了Elasticsearch这个,他也是以Lucene为基础的. 我去搜了几 ...

  10. FastJson简单使用

    首先建立两个实体类,Student.java 和 Teacher.java public class Student { private int id; private String name; pr ...