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. 【SDOI2017】新生舞会

    题面 题解 一眼\(0/1\)分数规划 二分答案\(mid\),我们要\(\sum\limits_i a^{'}_i - mid\sum\limits_i b_i^{'}\)最大 那么我们将\(a_{ ...

  2. SQL Server 创建带返回值的存储过程

    --drop procedure zcstest; create procedure zcstest ( @tableName varchar(max), @dataCount int output ...

  3. c#使用 HtmlAgilityPack来进行抓取和解析来获得table表格信息

    项目上要用到抓取网页,最后选用了HtmlAgilityPack来进行. 官网地址:https://html-agility-pack.net/,可以看一下 基础的: // From File var ...

  4. SpringCloud-微服务配置统一管理SpringCloud Config(七)

    前言:对于应用,配制文件通常是放在项目中管理的,它可能有spring.mybatis.log等等各种各样的配置文件和属性文件,另外你还可能有开发环境.测试环境.生产环境等,这样的话就得一式三份,若是传 ...

  5. 联赛emacs配置

    (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you co ...

  6. linux 之 jq

    1.安装 mac 安装: brew install jq centos 安装: yum install jq ubuntu: 安装: apt-get install jq 2.使用 cat test. ...

  7. XAF-由于try catch导致的性能问题一例

    前几天在制作PMMS系统时,有天突然发现性能问题下降严重,发布到客户机后,每点击一个按钮要花5-10秒的时间,与本机的200-600毫秒差距很大. 经过多处优化后没有效果. 后来想起,最近增加的功能是 ...

  8. 3、ObjectARX开发创建直线、圆、圆弧和修改对象属性

    一.本节课程 Arx二次开发创建直线.圆.圆弧和修改对象属性 二.本节要讲解的知识点 1.如何应用C++ ARX二次开发创建直线. 2.如何应用C++ ARX二次开发创建圆. 3.如何应用C++ AR ...

  9. Git知识点整合

    Git安装 Windows上安装Git 64 位安装包下载地址 : https://github.com/git-for-windows/git/releases/download/v2.16.2.w ...

  10. 虚拟机中安装MAC OS X教程(适用所有电脑方法,特别是cpu不支持硬件虚拟化的电脑)

    前言 之前写了一篇在Windows上搭建Object-C开发环境,并且写了一个HelloWorld程序.但真正开发苹果软件是在MAC OS X系统中(以下简称OSX)中.买不起MacBook,也没有O ...