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

解答:K 如果大于length,对 k 求余

public class Solution {
public int[] rotate(int[] nums, int k) {
if(nums == null){
return nums;
}
k = k % nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k -1);
reverse(nums, k, nums.length - 1);
return nums;
} 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--;
} }
}

189. Rotate Array的更多相关文章

  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. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  3. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

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

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

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

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

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

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

随机推荐

  1. 5. 网络配置与FTP服务笔记

    IP地址: Ipv4        2*32       Ipv6 tcp      网络通讯协议 udp    用户数据报协议 常见网络端口: 20  21      ftp服务 文件共享 22   ...

  2. github 添加 SSH key

    在 github 上添加 SSH key 的步骤: 1.首先需要检查你电脑是否已经有 SSH key 运行 git Bash 客户端,输入如下代码: $ cd ~/.ssh $ ls 这两个命令就是检 ...

  3. JS写随机数

    使用JS编写一个方法 让数组中的元素每次刷新随机排列(不得使用sort方法:需注明步骤思路).例如:初始数组:a,b,c,d 每次刷新页面都会显示不同:b,c,d,a….a,d,c,b…等等 代码: ...

  4. 嵌入式Linux > 简易安装思路,步骤记录

    思路就是把ipk文件通过ftp上传到linux上,然后通过opkg来安装. 很多的工具已经都能UI化这些操作了,命令行只是备用的工具, - 当所有工具都不好用或者遇到问题的时候,上命令行是最直接.快捷 ...

  5. 从零开始学习Android(二)从架构开始说起

    我们刚开始学新东西的时候,往往希望能从一个实例进行入手学习.接下来的系列连载文章也主要是围绕这个实例进行.这个实例原形是从电子书<Android应用开发详解>得到的,我们在这里对其进行详细 ...

  6. django queryset values&values_list

    values返回是字典列表; values_list返回的是元组列表, values_list加上 flat=True 1 1 之后返回值列表

  7. T-SQL编程 —— 用户自定义函数(标量函数)

    用户自定义函数 在使用SQL server的时候,除了其内置的函数之外,还允许用户根据需要自己定义函数.根据用户定义函数返回值的类型,可以将用户定义的函数分为三个类别: 返回值为可更新表的函数 如果用 ...

  8. Javascript学习笔记:3种检测变量类型的方法

    ①typeof检测变量类型 console.log(typeof 1);//number console.log(typeof "a");//string console.log( ...

  9. 16-网易-intership

    1.多选 //HTML <p>很长的一段文字,很长的一段文字,很长的一段文字,特别长的文字</p> //CSS p{ width:100px; white-space:nowr ...

  10. ueditor .net版本上传图片功能配置

    1.官网下载 UEditor .Net最新版:官方网址:http://ueditor.baidu.com/website/download.html#ueditor   UTF-8版 和GBK版选择疑 ...