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 ...
随机推荐
- Spark Mllib里如何程序输出数据集的条数(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集
- 《移动Web前端高效开发实战》笔记4--打造单页应用SPA
路由是一个单页应用的核心,大部分前端框架都实现了一个复杂的路由库,包括动态路由,路由钩子,组件生命周期甚至服务器端渲染等复杂的功能.但是对于前端开发者而言,路由组件的核心是URL路径到函数的映射,了解 ...
- 动态代理案例1:运用Proxy动态代理来增强方法
动态代理案例1: /*要求:运用Proxy动态代理来增强方法 题目: 1.定义接口Fruit,其中有addFruit方法 2.定义实现类FruitImpl,实现Fruit接口 3.定 ...
- uvm_reg_cbs——寄存器模型(十六)
当你完成寄存器模型的时候,你就会想到给后来的人一个接口,给他更多的扩展,让他做更多的事,一般而言,只有做VIP时,会想到做callbacks. typedef class uvm_reg; typed ...
- 【虚拟机-网络IP】如何开放 Azure 虚拟机 Ping 功能
前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...
- HDU 5500 Reorder the Books (水题)
题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...
- 在2017年,如何将你的小米4刷上Windows 10 mobile?(后附大量图赏)
众多攻略集大成者!资深软粉亲测有效! 参考教程: http://bbs.xiaomi.cn/t-11814358 http://bbs.xiaomi.cn/t-11736827 问:刷机前,我需要做什 ...
- Distributed Transaction Coordinator(DTC)一些问题的解决方法
有时运行某个程序或者安装SQL Server时报错. 错误信息: 事务管理器不可用.(从 HRESULT 异常: 0x8004D01B) 启动服务Distributed Transaction Coo ...
- 记一次RabbitMq 安装和配置坑
记一次RabbitMq 安装和配置坑 正常情况下安装 先安装erl ,在安装rabbitmq 这个在windows下的安装没什么技巧,按照默认一路下一步就ok.安装好后可以到cmd测试是否安装好. 测 ...
- java面试题(杨晓峰)---第五讲String、StringBuffer、StringBuilder有什么区别?
线程 字符 操作频繁度 1 String (1)String的创建机制 由于String在java世界中使用过于频繁,java为了避免在一个系统中产生大量重复的String对象,引入了字符串常量池,其 ...