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 ...
随机推荐
- day39-异常处理
一.程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 def test: pass ...
- MySql.Data.MySqlClient连接MySql
在C#中连接MySql数据库其实是件很简单的事情,但对于刚开始学习C#的朋友来说,问题却是不小,主要原因是相对于ACCESS和MSSql来说,MySql方面的教程文章实在太少,我也是自己摸索好好半天才 ...
- eclipse,import,导入项目显示红色叹号
[场景]eclipse导入项目后,该项目出现红色叹号.打开类文件,会有无数的“The type ... cannot be resolved.”等稀奇古怪的编译错误. [分析]当前eclipse环境中 ...
- idea 自动导入包和自动将没用的包去除
加快开发效率,除去没用的包,洁癖者必用! 这样设置,就可以自动导入包以及除去没有用到的包
- Activity生命周期,切换,参数传递,bundle(包),值对象,Activity参数返回,Activity的启动模式
Activity代表手机屏幕的一屏,或是平板电脑中的一个窗口.它是android应用中最重要的组成单元之一,提供了和用户交互的可视化界面.在一个Activity中,可以添加很多组件,这些组件负责具体的 ...
- MOBA项目定点数的一个想法
能不能这样: 写逻辑时全用整数,不用每用到一个浮点数就要转一下成浮点数. 主要是除法 题细节较多,待思考
- Java IO流学习总结二:File
Java File类的功能非常强大,利用java基本上可以对文件进行所有操作.首先来看File类的构造函数的源码 /** * Internal constructor for already-norm ...
- css实现角标
效果图: 简单方式可以使用背景图片,但这里我使用的css来实现,最笨的方式是使用矩形div然后旋转遮挡就可以, <div class='checked-item'> 角标实现 < ...
- Oracle快速导入数据工具
sqlldr是oracle自带的快速导入批量数据的工具,常用于性能测试.考虑手工构造控制文件较为繁琐,因此使用脚本完成批量数据的自动导入. 基本知识 sqlldr命令语法 sqlldr dbname/ ...
- 注解(Annotation)是什么?