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.
(1)
/*
* this solution is so-called three times rotate method
* because (X^TY^T)^T = YX, so we can perform rotate operation three times to get the result
* obviously, the algorithm consumes O(1) space and O(n) time
*/
void rotate(int nums[], int n, int k) {
if (k<=) return;
k %= n;
reverseArray(nums, n-k, n-);
reverseArray(nums, , n-k-);
reverseArray(nums, , n-);
}
(2)
/*
* How to change [0,1,2,3,4,5,6] to [4,5,6,0,1,2,3] by k = 3?
*
* We can change by following rules:
*
* [0]->[3], [3]->[6], [6]->[2], [2]->[5], [5]->[1], [1]->[4]
*
*
*/
void rotate(int nums[], int n, int k) {
if (k<=) return;
k %= n;
int currIdx=, newIdx=k;
int tmp1 = nums[currIdx], tmp2;
int origin = ;
for(int i=; i<n; i++){
tmp2 = nums[newIdx];
nums[newIdx] = tmp1;
tmp1 = tmp2;
currIdx = newIdx;
//if we meet a circle, move the next one
if (origin == currIdx) {
origin = ++currIdx;
tmp1 = nums[currIdx];
}
newIdx = (currIdx + k) % n;
}
}
189. Rotate Array -- 将数组前一半移到后一半的更多相关文章
- [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 旋转数组
将包含 n 个元素的数组向右旋转 k 步.例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 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 Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- Rotate Array 旋转数组 JS 版本解法
Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...
- 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 ...
随机推荐
- [HDOJ5952]Counting Cliques(DFS,剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5952 题意:求图中规模为s的团的个数. DFS+剪枝,姿势不好很容易TLE啊. #include &l ...
- SqlServer 存储过程分页
适用于2005以上版本 create procedure [dbo].[SP_GetPageList] ( @columns nvarchar(max), --查询字段 @tablename nvar ...
- 二叉树hdu1710
学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...
- python_way day15 HTML-DAY2 HTML-DAY2、JS
python_way day15 HTML-DAY2 html-css回顾 javascript 一.html-css回顾 1.input与+,-号的写法 <!DOCTYPE html> ...
- XAF Excel数据导入模块使用说明与源码
我实现了XAF项目中Excel数据的导入,使用Devexpress 新出的spreadsheet控件,可能也不新了吧:D 好,先看一下效果图:下图是Web版本的. 下面是win版: 功能说明: 支持从 ...
- 关于linux 卸载问题
网上找了一套引擎 非用protocbuff 2.4.1 结果机器上已经装好了2.6.1 网上找了好多办法都行不通 最后终于在一个群里问到 mark一下 例如 我想卸载当前得protoc 那么 第一步 ...
- Linux_常用命令_03_磁盘/挂载_信息查看
1. 1.1. mount 不带参数的话,显示的是 当前已经挂载的情况 1.2. df 不带参数的话,硬盘分区状况查询 2. 2.1. cat /proc/partitions 2.2. fdisk ...
- web设计经验<三>值得你深入了解的交互设计5大支柱
随着单页式设计和移动端的兴起,网页中的交互设计越来越重要了.为了打造流畅而可靠的用户体验,你需要对交互设计有更加深入的了解. 正如同我们在<交互设计最佳实践(卷1)>中所述,要做好交互设计 ...
- placeholder在不同浏览器下的表现及兼容方法(转)
1.什么是placeholder? placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...
- poj1066Treasure Hunt(线段相交)
链接 很纠结的找到了所有线段的中点,又很纠结的找到了哪些中点可以直接相连,最后bfs一下求出了最短路.. #include <iostream> #include<cstdio> ...