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.

[show hint]

Related problem: Reverse Words in a String II

 class Solution {
public void rotate(int[] nums, int k) {
k%=nums.length;
if(k==0) return; reverce(nums,0,nums.length-1);
reverce(nums,0,k-1);
reverce(nums,k,nums.length-1);
}
private void reverce(int[] a,int i,int j){
for(int k = 0;k <= (j-i)/2;k++){
swap(a,i+k,j-k);
}
}
private void swap(int[] a,int i ,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

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

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

  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. 【easy】189. Rotate Array

    题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...

  9. LeetCode Array Easy 189. Rotate Array

    ---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...

随机推荐

  1. datatable详解(angular-datatable)+后台分页(springmvc)

    datable常规配置,百度一大堆 function prepareDatatable(selector, options) { var defaultOptions = { autoWidth: t ...

  2. 去除 \ufeff

    语言:python 编程工具:pycharm 硬件环境:win10 64位 读取文件过程中发现一个问题:已有记事本文件(非空),转码 UTF-8,复制到pycharm中,在开始位置打印结果会出现  \ ...

  3. Python3x 爬取妹子图

    思路:1.get_totalpages(url)  通过[性.感.美.女.图]获得该版块的总页数 [首页1234567891011下一页末页共 21页1034条] 2.get_sercoverurl( ...

  4. ASp.Net控件的生命周期

    服务端事件 页面生命周期 描述 Init Initialization 初始化控件树 LoadViewState Unpack ViewState 从ViewState里提取出状态信息 LoadCon ...

  5. linux C之alarm函数 转

    原文出处:http://blog.sina.com.cn/s/blog_6a1837e90100uhl3.html alarm也称为闹钟函数,alarm()用来设置信号SIGALRM在经过参数seco ...

  6. UE4与Visual Studio版本对应关系

    首先,要注意,Visual Studio 2015(VS2015)默认安装时并不包含 C++ 的工具.在安装 VS2015 时,需选择 自定义 安装并勾选 C++ 组件,这对之后的工作时必须的.如果已 ...

  7. Linux命令之乐--test

    官方文档: help test File operators: -a FILE True if file exists. -b FILE True if file is block special. ...

  8. hammer.js移动端手势库

    hammer.js 是一个多点触摸手势库,能够为网页加入Tap.Double Tap.Swipe.Hold.Pinch.Drag等多点触摸事件,免去自己监听底层touchstart.touchmove ...

  9. 《从零开始学Swift》学习笔记(Day 70)——Swift与Objective-C混合编程之Swift与Objective-C API映射

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift与Objective-C API映射 在混合编程过程中Swift与Objective-C调用是双向的,由于不同语言对于相同API的表述是不同的, ...

  10. powerdesigner 导入SQL脚本生成模型