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 ...
随机推荐
- 超全面的vue.js使用总结
一.Vue.js组件 vue.js构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue ...
- linux中c表示字符设备文件符号
linux中c表示字符设备文件,b表示块设备文件,l表示符号链接文件,r表示可读权限,w表示可写权限.linux文件属性解读:文件类型:-:普通文件 (f)d:目录文件b:块设备文件 (block)c ...
- dubbo服务降级(1)
1. 在 dubbo 管理控制台配置服务降级 上图的配置含义是:consumer 调用 com.zhang.HelloService 的方法时,直接返回 null,不发起远程调用. 实际操作是:在 z ...
- Ubuntu 11.04 安装 cuda5.0
由于实验需要,于2016年10月15日再Ubuntu11.04安装cuda5.0,但是从网上查找Ubuntu11.04 只有对应的支持的cuda4 版本,cuda 5.0前面版本不支持IDE nisg ...
- awk对列求和
awk 'BEGIN{total=0}{total+=$1}END{print total}' 文件名
- UVA 674 Coin Change 硬币转换(完全背包,常规)
题意:有5种硬币,个数无限的,组成n元的不同方案有多少种? 思路:常规完全背包.重点在dp[0]=1,dp[j]中记录的是组成 j 元的方案数.状态转移方程dp[j+coin[i]]+=dp[j]. ...
- HDU 5094 Maze (状压)
加一个维度,钥匙的状态,状压一下.n很小,钥匙也只有10个,bfs就好了. 忘了数组初始化.以后坚决不犯这种低级错误. #include<cstdio> #include<queue ...
- Android(java)学习笔记115:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...
- 原型模式 -- JavaScript语言的灵魂
原型模式就是将原型对象指向创建对象的类,使这些类共享原型对象的方法与属性.JS是基于原型链实现对象之间的继承,是对属性或者方法的共享,而不是对属性和方法的复制. // 图片轮播类 var LoopIm ...
- Java压缩字符串工具类
StringCompressUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.By ...