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

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

This is same as Reverse Words in a String.  (https://leetcode.com/problems/reverse-words-in-a-string/)

 public class Solution {
public void rotate(int[] nums, int k) {
if(nums == null || nums.length == 0) return;
int len = nums.length;
k %= len;
reverse(nums, 0, len - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, len - 1);
} public void reverse(int[] arr, int start, int end){
while(start < end){
int tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
start ++;
end --;
}
}
}

Rotate Array II的更多相关文章

  1. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  2. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  3. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  6. LeetCode189——Rotate Array

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

  7. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  8. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. Leetcode-189 Rotate Array

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

随机推荐

  1. 【BZOJ1047】[HAOI2007]理想的正方形

    [BZOJ1047][HAOI2007]理想的正方形 题面 bzoj 洛谷 题解 二维\(st\)表,代码是以前的 #include<iostream> #include<cstdi ...

  2. [HNOI2013]比赛 搜索

    [HNOI2013]比赛 搜索. LG传送门 直接暴力有60,考场上写的60,结果挂成40. 考虑在暴力的同时加个记忆化,把剩下的球队数和每支球队的得分情况hash一下,每次搜到还剩\(t\)个队的时 ...

  3. Print Spooler 服务自动停止

    1)先在服务里停止并禁用Print Spooler : 2)删除此文件夹下的所有文件,C:\Windows\System32\spool\PRINTERS\ : 3)删除注册表 HKEY_LOCAL_ ...

  4. javaweb学习3——验证码

    声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...

  5. 通过切换iframe来定位元素(用于Python+selenium自动化测试)

    切换 iframe:1.由于登录按钮是在iframe上,所以第一步需要把定位器切换到iframe上2.用switch_to_frame方法切换,此处有id属性,可以直接用id定位切换 iframe 与 ...

  6. 增强学习训练AI玩游戏

    1.游戏简介 符号A为 AI Agent. 符号@为金币,AI Agent需要尽可能的接取. 符号* 为炸弹,AI Agent需要尽可能的躲避. 游戏下方一组数字含义如下: Bomb hit: 代表目 ...

  7. C/S结构与B/S结构

    按照是否需要访问网络,程序可分为网络程序与非网络程序.其中网络程序又可分为B/S结构与C/S结构. C/S结构是指客户端(Client)/服务器(Server)模式,这种模式的客户端中 需要安装一个R ...

  8. Python科学测量与计算库Pymeasure: 控制你的仪器进行自动测试和科学计算

    Python这种脚本语言因其语法简单,工具包丰富成熟,使用起来非常方便.在很多领域被广泛使用,今天介绍的是python在仪器控制应用领域,python在仪器控制领域相关的书籍可以参考<真实世界的 ...

  9. IOS git 删除仓库 新建仓库 提交 合并 操作 码云

    HDHaoShaoPengdeiMac:~ hdhaoshaopeng$ defaults write com.apple.finder AppleShowAllFiles TRUE HDHaoSha ...

  10. mkfs命令详解

    mkfs命令-->make filesystem的缩写:用来在特定的分区建立Linux文件系统     [命令作用] 该命令用来在特定的分区创建linux文件系统,常见的文件系统有ext2,ex ...