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 [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.
[show hint]
Hint:
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位。题目不难,但是需要注意的一点是k值可能大于序列长度,所以,旋转之前先求k对序列长度size的余数即可。
该题目一个简单的解决方法是三次反转:
- 将序列中(0 , size-k-1)元素反转;
- 将序列中(size-k , size-1)元素反转;
将全序列(0,size-1)反转;
即可完成要求!
AC代码
class Solution {
public:
void rotate(vector<int>& nums, int k) {
if (nums.empty())
return;
int size = nums.size();
//确定最终右旋元素个数
k %= size;
vector<int>::iterator beg = nums.begin();
//旋转0~(size-k) (size-k , size);
reverse(beg, beg + size - k);
reverse(beg + size - k, nums.end());
//最后一次全部反转,即可完成
reverse(beg, nums.end());
}
};
LeetCode(189) Rotate Array的更多相关文章
- LeetCode(61) Rotate List
题目 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Giv ...
- (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(48)Rotate Image
题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
随机推荐
- Net Core -- 配置Kestrel端口
Net Core -- 配置Kestrel端口 Kestrel介绍 在Asp.Net Core中,我们的web application 其实是运行在Kestrel服务上,它是一个基于libuv开源的跨 ...
- Redis 基础特性讲解
目录 1.Redis基础杂项小节 1.是什么 2.能干嘛 3.去哪下 4.Redis启动后基础知识讲解 2.Redis数据类型 1.常用的五大数据类型 2.高级'玩家'才知道的其他数据类型 3.Red ...
- .net的基础知识点
在这个It市场都是风云变化的,都是又市场供需来定的,当年iOS火的一塌糊涂的,现在也出现找不到工作的,满地的出入门者,我就属于其中一个,在一个逼不得已的情况下,开始转行做.net ,我相信当年的很多. ...
- css:hover伪类的使用
:hover的使用,即当鼠标指针移入元素时,所做出的样式设置 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...
- Windows下Python多版本共存
Windows下Python多版本共存 Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 0.0 因为公司项目,需要Python两个 ...
- bootstrap中container和container-fluid的区别与用法
对bootstrap框架有一定了解的朋友都知道,一般页面布局中的开头会使用到container或container-fluid类,那么它们有什么区别呢?不急!下面为您讲解. 我们先来看看官方对这两个类 ...
- Android 模仿苹果虚拟悬浮按钮(自动靠边、可浮现任何界面上)
由于最近小蔡的手机音量键坏了,调节音量有点麻烦,突发奇想,想自己实现一个快捷键来调节音量.在忘上参考了一些代码,总结出一般本章,分享给大家. 首先 按钮要想实现悬浮在任何界面,那么必须是要写在服务里面 ...
- Class 类
在javascript 中应用类的概念 // javascript web applications 富应用开发 // 类库:生成类的地方:给所有的构造函数提供基础方法,如 extend, inclu ...
- django之母版的继承
模板继承示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- BZOJ 2654: tree Kruskal+二分答案
2654: tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1863 Solved: 736[Submit][Status][Discuss ...