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 ...
随机推荐
- RE模块错误已解决.
下面这个错误是由于在正则[...]的内部,减号'-'是一个有特殊含义的字符(代表字符范围) 所以如果需要在[...]内匹配减号'-',需要用反斜杠'\'转义. >>> import ...
- memcached实战系列(一)memcached安装
下载并安装Memcached服务器端 我用的是cenos6.5 64位系统. libevent是个程序库,它将Linux的epoll.BSD类操作系统的kqueue等事件处理功能封装成统一的接口,具有 ...
- JDBC-数据库的更新操作编程(三)
首先建立一个静态方法,代码如下: public static Statement getStatement(){ Statement st = null; try { Class.forName(&q ...
- Unity插件 - MeshEditor(七)变形动画骨骼及蒙皮
MeshAnimation在物体的顶点比较多的情况下,悲剧是显而可见的,我一个一个的点选顶点肯定得累死,而且对于形态的调控不是很方便,应该说是很麻烦,要知道,骨骼动画因为有了骨骼以及蒙皮信息而有了灵魂 ...
- TCP的发送系列 — tcp_sendmsg()的实现(一)
主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 上一篇blog讲的是send().sendto().sen ...
- Swift基础之Animation动画研究
最近研究了一下,Swift语言中关于Animation动画的实现学习,分两次进行相关内容的讲解 用表格列出各种动画情况 Demo首页显示展示了一种动画显示方式,代码如下: //绘画装饰 func ...
- iOS开发之八:UISlider、UISegmentedControl、UIPageControl的使用
本文的三种控件,用的也非常多,而我也是经常图懒,而去打开原来的项目去拷贝,现在记录一下,就不用去项目中去找这些控件的用法了. 一.UIActivityIndicatorView 的使用 UIActiv ...
- Cocos2D中的纹理(textures)的解释
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- Ubuntu中firefox设置成中文
进入 http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly 按版本选择下去,帮助(help)-->关于,查看浏览器的版本号 所以,目录是3 ...
- C#attribute-----------初级
前言: attribute是 .net FrameWork 提出的技术,可以为自己的代码添加注解,从而实现些特殊功能. 一. attribute功能 attribute被译作特性,既然是特性,必然功能 ...