LeetCode之“数组”: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].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
这道题要注意的是:k可能比数组的大小还大。程序如下:
class Solution {
public:
void permute(vector<int>& nums, int start, int end)
{
if(start > end)
return;
int sz = (end - start) / + ;
for(int i = ; i < sz; i++)
{
int tmp = nums[start+i];
nums[start+i] = nums[end-i];
nums[end-i] = tmp;
}
}
void rotate(vector<int>& nums, int k) {
int sz = nums.size();
if(sz <= k)
k -= sz;
if(k == )
return;
permute(nums, , sz - k - );
permute(nums, sz - k, sz - );
permute(nums, , sz - );
}
};
LeetCode之“数组”:Rotate Array的更多相关文章
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- LeetCode OJ:Rotate Array(倒置数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...
- [Swift]LeetCode189. 旋转数组 | 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 解题报告(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 ...
- LeetCode算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- (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 ...
- 【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 ...
随机推荐
- Excel 数据验证宏
Sub 宏1() ' ' 宏1 宏 ' ' With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlVal ...
- HTML简单使用
HTML简单使用 标签 : 前端技术 HTML HTML(Hypertext Marked Language), 即超文本标记语言,能够独立于各种操作系统平台(如UNIX/Linux/Windows等 ...
- activiti实战系列 activiti连线
11:连线 11.1:流程图 注意:如果将流程图放置在和java类相同的路径,需要配置: 11.2:部署流程定义+启动流程实例 11.3:查询我的个人任务 11.4:完成任务 说明: 1)使用流程变量 ...
- JAVA面向对象-----接口与类、接口之间的关系
接口与类.接口之间的关系 大家之前都知道类与类之间的关系继承,那么接口与类之间又是怎样子的关系呢? 接口与类之间是实现关系.非抽象类实现接口时,必须把接口里面的所有方法实现.类实现接口用关键字impl ...
- Git 解决一个电脑多用户情况(win7)
首先:在输入ssh-keygen -t rsa -C "注册邮箱"后不要急着按enter,此时输入秘钥对的文件名,不要跟默认文件重名(默认的是id_rsa) 划红线的地方就是新的文 ...
- Python读取JSON数据,并解决字符集不匹配问题
今天来谈一谈Python解析JSON数据,并写入到本地文件的一个小例子. – 思路如下 从一个返回JSON天气数据的网站获取到目标JSON数据串 使用Python解析出需要的部分 写入到本地文件,供其 ...
- bash与ksh数组使用
区别: bash与ksh在数组的使用中,最大的不同在于数组的定义. bash: declare -a arrayname ksh:set -A arrayname 其实,数组不用非要定义,在赋值的时候 ...
- iOS中的两种搜索方式UISearchDisplayController和UISearchController
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...
- 如何找到java对应的c/c++源码
很多时候java经常被c鄙视,因为c说我是你的基础,但java竟然有如此强的生命力就必然有其存在的价值.本文不探讨各种开发语言的优劣,仅仅介绍如何找到java对应c/c++实现的源码.当我们追究一个j ...
- UNIX网络编程——TCP—经受时延与nagle算法、滑动窗口、拥塞窗口
1.经受时延: TCP在接收到数据时并不立即发送ACK,相反,它推迟发送,以便将ACK与需要沿该方向发送的数据一起发送,时延为200ms,超过时延范围,发送确认. 2.nagle算法: 一个TCP连接 ...