LeetCode & Q189-Rotate Array-Easy

时间:2023-01-07 11:08:24

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. &lbrack;LeetCode&rsqb; 189&period; 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&period; 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 &lbrack;Leetcode 189&rsqb;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&num;解leetcode 189&period; 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&lpar;67&rpar;-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&period; Non-decreasing Array&lpar;Easy&rpar;

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

随机推荐

  1. ASP&period;NET corrupt assembly &OpenCurlyDoubleQuote;Could not load file or assembly App&lowbar;Web&lowbar;&ast;

    以下是从overFlow 复制过来的问题 I've read through many of the other questions posted on the same issue, but I s ...

  2. quartz(1)

    关于定时任务的操作方法,java语言本身具有 Timer 来解决,但Timer 作用起来不是特别的舒服,由于项目的需要,使用了Quartz 这个调度框架,现把学习过程记录下来,方便以后查阅. 本教程是 ...

  3. Atom&period;io设置ctrl&plus;delete

    一般常见的text editor,在文本前面的空白处按下ctrl+delete,只是删除空白符到单词前面停下,但是Atom.io的默认设置,把空白符后遇到的第一个单词也删掉了.改配置方法是在keyma ...

  4. AR

    http://jingyan.baidu.com/article/6766299727dcfc54d41b8455.html 1.注册.然后下载sdk(注册账号主要是为了第3步中制作识别图而用的) 下 ...

  5. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A * regime is trying to redistribute wealth in a village. They ...

  6. CentOS上编译安装Git

    1. 安装(编译安装)软件 # 先安装git依赖的包 yum install zlib-devel yum install openssl-devel yum install perl yum ins ...

  7. VMware Workstation 9&period;0 安装苹果Mac OS X10&period;9系统

    摘自:http://www.wuwenhui.cn/3133.html 一.安装所需要的软件: 1.VMware Workstation 9.0 点击下载 2.unlock-all-v110.zip  ...

  8. 【翻译mos文章】Linux x86 and x86-64 系统SHMMAX最大

    Linux x86 and x86-64 系统SHMMAX最大值 参考原始: Maximum SHMMAX values for Linux x86 and x86-64 (文件 ID 567506. ...

  9. Android笔记&colon; fragment简单例子

    MainActivity.java public class MainActivity extends Activity { @Override protected void onCreate(Bun ...

  10. ubuntu12&period;04添加程序启动器到Dash Home

    ubuntu12.04 dash home中每个图标对应/usr/share/applications当中的一个配置文件(文件名后缀为.desktop).所以要在dash home中添加一个自定义程序 ...