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 ...
随机推荐
- 机器学习进阶-边缘检测-Canny边缘检测 1.cv2.Canny(进行Canny边缘检测)
1. cv2.Canny(src, thresh1, thresh2) 进行canny边缘检测 参数说明: src表示输入的图片, thresh1表示最小阈值,thresh2表示最大阈值,用于进一步删 ...
- UI5-学习篇-16-云端SCP-Destination配置
1.登录路径: https://account.ap1.hana.ondemand.com/#/home/welcome 2.查看云连接器 如下图所示,虚拟主机都已连接且资源可用: 若虚拟主机连接存在 ...
- (原)Echarts 报Uncaught Error: Initialize failed: invalid dom 根本解决
1.循环出的Echarts出现 Uncaught Error: Initialize failed: invalid dom ,附上完美解决方案 setTimeout(function () { co ...
- 批量杀死多个进程 linux kill
批量杀进程 -| “grep -v grep”是在列出的进程中去除含有关键字“grep”的进程. “cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID,也有使用aw ...
- javascript中函数作用域和声明提前
javascript不像java等其他强类型语句,没有块级作用域(括号内的代码都有自己的作用域,变量在声明它们的代码段之外不可见)一说,但有自己的独特地方,即函数作用域. 函数作用域:变量在声明它们的 ...
- angular指令的compile,prelink 和 postlink以及controller
一. 指令模板选项有complie和link两个字段,两者之间存在如下关系: 当compile字段存在时,link字段将被忽略,compile函数的返回值将作为link字段. 当compile不存在, ...
- mysql主从复制搭建中几种log和pos详解
一.主从原理 Replication 线程 Mysql的 Replication 是一个异步的复制过程,从一个 Mysql instace(我们称之为 Master)复制到另一个 Mysql in ...
- 浅谈MySQL事务及隔离级别
目录 1.什么是事务 2.事务的ACID属性 2-1.原子性(Atomicity) 2-2.一致性(Consistency) 2-3.隔离性(Isolation) 2-4.持久性(Durability ...
- HttpURLConnection类的使用
此类以获取天气的一个api地址为例: package javaexcjs; import java.io.BufferedReader; import java.io.OutputStreamWrit ...
- 18.异常.md
目录 1.try...catch 2.异常了的继承机制 2.1基本概念 2.2常用异常 2.3多异常捕获 2.4获取异常信息 2.5finally回收资源 2.6Checked异常和Runtime异常 ...