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 ...
随机推荐
- 【转】 史上最详尽的平衡树(splay)讲解与模板(非指针版spaly)
ORZ原创Clove学姐: 变量声明:f[i]表示i的父结点,ch[i][0]表示i的左儿子,ch[i][1]表示i的右儿子,key[i]表示i的关键字(即结点i代表的那个数字),cnt[i]表示i结 ...
- MySql必知必会实战练习(三)数据过滤
在之前的博客MySql必知必会实战练习(一)表创建和数据添加中完成了各表的创建和数据添加,MySql必知必会实战练习(二)数据检索中介绍了所有的数据检索操作,下面对数据过滤操作进行总结. 1. whe ...
- 接口测试基础——第4篇logging模块
Logging:日志记录是为了跟踪记录软件运行时,发生的事件,包括出错,提示信息等等. log日志级别:日志级别大小关系为:CRITICAL > ERROR > WARNING > ...
- Jquery3.x高版本支持IE8
最近在做项目的时候,遇到一个安全漏洞的问题 检测到目标站点存在javascript框架库漏洞 解决办法是 将受影响的javascript框架库升级到最新版本. 好吧,就给你升吧,升完之后,我的天啊,尽 ...
- Java 数组的定义和遍历
1.一维数组 数组是用来存储一组相同数据类型数据的数据结构 数组的元素可以是简单数据类型的数据,也可以是引用数据类型的数据 无论数组内容是简单类型还是引用类型,数组自己本身都是一种引用类型 每个数组元 ...
- 深入浅出K-Means算法
在数据挖掘中,K-Means算法是一种cluster analysis的算法,其主要是来计算数据聚集的算法,主要通过不断地取离种子点最近均值的算法. 问题 K-Means算法主要解决的问题如下图所示. ...
- 中间件——Oracle Fusion Middleware
Oracle Fusion Middleware定义: 什么是Oracle Fusion Middleware Oracle Fusion Middleware is a comprehensive ...
- 测试php中的curl是否可使用
<?php $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,"http://www.baidu.com"); curl_seto ...
- mysql设计与开发
架构设计表结构设计索引sql语句1.表结构设计的核心思想是什么?容量评估,性能优化,硬件升级,垂直拆分,水平拆分 2.有个大表为了一个查询(一天就查2次),领导要你建索引(索引空间大小有500G),你 ...
- 如何成为java架构师(转载)
链接:https://www.zhihu.com/question/29031276/answer/54631312 来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1 ...