189. Rotate Array(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.
Hint:
Could you do it in-place with O(1) extra space?
思路:通过三次翻转来实现移位
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int size = nums.size();
k %= size;
if(k == size || k == ) return;
reverse(nums,,size--k);
reverse(nums,size-k,size-);
reverse(nums, , size-);
return;
}
void reverse(vector<int>& nums, int start, int end){
int tmp;
while(start < end){
tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
};
189. Rotate Array(Array)的更多相关文章
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 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 ...
- Java for 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 ...
- 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 ...
- Java [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 ...
- C#解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 ...
随机推荐
- kubernets之endpoints
注:本文整理自网络 endpoint endpoint是k8s集群中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址.service配置selector,endp ...
- 【ASP.NET 插件】Plupload多格式多文件上传实现
由于工作需求,要实现一个多格式多文件的上传功能,而且需要.NET版本的,嘿嘿,终于还是实现了,网上搜了很久,找到一篇不错的博文:WEB版一次选择多个文件进行批量上传(Plupload)的解决方案,在此 ...
- mac 关于默认python2下的pip,和python3下pip 的坑
pip是常用的python包管理工具,类似于java的maven.用python的同学,都离不开pip. 1.在Python2.7的安装包中,easy_install.py是默认安装的,而pip需要手 ...
- day41-解决粘包问题
一.socket缓冲区 研究粘包之前先看看socket缓冲区的问题: 二.socket缓存区的详细解释 每个socket被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区. write()/send ...
- centos7 自动定时备份mysql数据库
shell脚本:mysqlbak.sh #!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbinexpo ...
- js人形时钟
https://blog.csdn.net/rsylqc/article/details/44808063 分享自:http://chabudai.org/blog/?p=59 在这个网站看到一个很有 ...
- C++11之for循环的新用法《转》
相关资料:https://legacy.gitbook.com/book/changkun/cpp1x-tutorial/details C++11之for循环的新用法 C++使用如下方法遍历一个容器 ...
- MySQL 创建自定义函数
语法:Create function function_name(参数列表)returns返回值类型 函数体 函数名,应合法的标识符,不应与系统关键字冲突. 一个函数应该属于某个数据库,可以使用db_ ...
- leetcode 错误题解,反面教材 Gas Station
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& c ...
- Pycharm 字体大小快捷方式设置
1.File->Settings 2.在搜索框搜索increase 点击Increase Font Size(增大字体)右键选择 Add Mouse Shortcut 然后按Ctrl并且鼠标滚轮 ...