[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

  1. swap函数都忘了,也是醉了
  2. k步可能很大,所以需要%=数组长度

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

从左往右的:

[一刷]:

  1. swap函数需要带入index的,所以内部i j 不需要初始化

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

reverse都是用swap函数,套路都一样

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

swap

public void swap (int[] nums, int i, int j) {
while (i < j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp; i++;
j--;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

swap的所有题

[代码风格] :

class Solution {
public void rotate(int[] nums, int k) {
//cc
if (nums == null || nums.length == 0) {
return ;
} //ini, k
k %= nums.length; //swap
swap(nums, 0, nums.length - k - 1);
swap(nums, nums.length - k, nums.length - 1);
swap(nums, 0, nums.length - 1);
//return
} public void swap (int[] nums, int i, int j) {
while (i < j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp; i++;
j--;
}
}
}

189. Rotate Array 从右边开始翻转数组的更多相关文章

  1. 189. Rotate Array - LeetCode

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

  2. 189. Rotate Array【easy】

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

  3. &lt;LeetCode OJ&gt; 189. Rotate Array

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

  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 (旋转数组)

    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. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

  8. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  9. LeetCode OJ 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. PS常用美化处理方法大全

    学习PS的同学都知道,我们日常生活中使用PS就是进行一些简单的图像美白,图像颜色的优化,其他的基本不用,在长时间的PS使用过程中本人总结了一些处理皮肤的方法,都是一些非常简单的方法,希望能够帮助那些刚 ...

  2. Fast Arrangement (线段树,延迟标志)

    个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦. 按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都 ...

  3. ZOJ-Big string(服气思维)

    个人心得:我在分治上看到的,但是感觉跟分治没关系,一眼想到斐波那契数可以找到此时n的字符串,但是无法精确到字母,题解的思路 真是令人佩服,以BA为基准,然后只要此时的长度大于7那么必然可以减去最大的斐 ...

  4. 调用 SSPI 失败,请参见内部异常 解决方法

    2017-11-12 12:49:53:706] OnServerConnectionAvailable error : System.Security.Authentication.Authenti ...

  5. Collections排序

    0.前言 ThreeSet的底层实现是红黑树,它在创建set的过程中实现排序.Collections.sort是在对整个集合进行排序,按道理来说使用TreeSet插入集合元素直至建立整个TreeSet ...

  6. Linux 驱动层实现阻塞和非阻塞

    linux应用层的函数默认是阻塞型的,但是要想真正实现阻塞,还需要驱动的支持才行. 例:open().scanf().fgets().read().accept() 等 1.默认情形,驱动层不实现阻塞 ...

  7. SQL Server 占用CPU较高的解决方法

    触发原因:月底系统结账的时候对ERP的操作较多,有用户反馈系统之间的数据传输很久没有同步.随即到服务器上查看,没有发现有程序导致的问题,看了一下CPU的使用率,发现SQL Server占用率在百分之九 ...

  8. python3 安装 past 包

    python3 安装 past 包 $ pip install future 错误现象 缺乏包的引用 from past.types import unicode 参考链接 https://pypi. ...

  9. Android Intent Action 大全

    1.Intent的用法: (1)Action跳转 1. 使用Action跳转,当程序AndroidManifest.xml中某一个 Activity的IntentFilter定义了包含Action,如 ...

  10. Fragment的陷阱:概述

    现在主流的APP都会使用到Fragment,相信你也一定使用过,今天为大家介绍一下我曾经踏过的一个关于Fragment的坑. 以前做过的一个项目,Fragment嵌套高德地图,当再次进入Fragmen ...