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,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
- swap函数都忘了,也是醉了
- k步可能很大,所以需要%=数组长度
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
从左往右的:

[一刷]:
- 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 从右边开始翻转数组的更多相关文章
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- 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 ...
- 189 Rotate Array 旋转数组
将包含 n 个元素的数组向右旋转 k 步.例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 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 ...
随机推荐
- 瀑布流下滑 发送ajax
<!DOCTYPE=html> <html> <head> <meta charset="UTF-8"> ...
- Block Towers (思维实现)
个人心得:这题没怎么看,题意难懂.后面比完再看的时候发现很好做但是怎么卡时间是个问题. 题意:就是有m个可以用2层积木的,n个可以用三层积木的,但是他们不允许重复所以可以无限添加. 比如 3 2 一开 ...
- admins.py总结比较,转
转:http://blog.csdn.net/pipisorry/article/details/46764495
- #51单片机#超声波测距(HC-SR04)的使用方法
#51单片机#超声波测距(HC-SR04)的使用方法
- 如何给 FastAdmin 单独设置域名
如何给 FastAdmin 单独设置域名 (声明:不建议给后台固定的域名,主要是安全问题) FastAdmin 是基于 ThinkPHP5 框架编写的,ThinkPHP 5 支持域名路由,可对模块单独 ...
- build RTK on ubuntu 16.04
RTK-1.4.0 InsightToolkit-4.12.2 ./cmake/nvcc-check.cmake line:86 commentout as #message(FATAL_ERROR ...
- Quartz.net 2.x 学习笔记01
Quartz.net 2.0 2012年4月9日发布了Released版本,到目前(2014-12-08)为止是2.3版 Quartz.net 项目地址:http://www.quartz-sched ...
- js大法处理无法点击的问题
js定位的其他方法:
- Java-Maven-Runoob:Maven 仓库
ylbtech-Java-Maven-Runoob:Maven 仓库 1.返回顶部 1. Maven 仓库 在 Maven 的术语中,仓库是一个位置(place). Maven 仓库是项目中依赖的第三 ...
- jenkins学习(1)
(1) 按照JAVA, 增加环境变量JAVA_HOME = C:\Program Files\Java\jdk1.8.0_31 增加环境变量CLASS_PATH = ...