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 ...
随机推荐
- 第一次写python爬虫
花了4天终于把写完了把国内的几个漏洞平台爬完了,第一次写py,之前一直都在说学习,然后这周任务是把国内的漏洞信息爬取一下.花了1天学PY,剩下的1天一个.期间学习到了很多.总结如下: ======== ...
- 6.Type and Member Basics
1.The Different Kinds of Type Members 1.Constants:a symbol that identifies a never-changing data val ...
- CUBRID学习笔记 36 在net中添加多行记录
using System.Data.Common; using CUBRID.Data.CUBRIDClient; namespace Sample { class Add_MultipleRows ...
- 《Linux内核设计的艺术》学习笔记(一)从开机加电到加载三个汇编源码
实验内核版本:0.11 ◆ 从开机到main函数的三步: ① 启动BIOS,准备实模式下的中断向量表和中断服务程序: ② 从启动盘加载OS程序到内存中,加载OS程序的工作就是利用第一步中的中断服务 ...
- SQL 语句集合
创建数据库 创建之前判断该数据库是否存在 if exists (select * from sysdatabases where name='databaseName') drop database ...
- javascript中的计时器
javascript中的定时器有两种:一种是一次性定时器,一种是可以持续使用的定时器: 1:一次性定时器setTimeout(a,b):兼容ie的任何版本 该方法接受两个参数,第一个是要执行的代码,第 ...
- struts2标签之列求和
struts2标签之列求和 <table width="100%" border="0" cellpadding="0" cellsp ...
- html5实现GIF动画!
代码如下: <!DOCTYPE html><html> <head> <meta charset="utf-8"&g ...
- (一)MII/MDIO接口详解
本文主要分析MII/RMII/SMII,以及GMII/RGMII/SGMII接口的信号定义,及相关知识,同时本文也对RJ-45接口进行了总结,分析了在10/100模式下和1000M模式下的设计方法. ...
- 本地存储 localStorage/sessionStorage/cookie
cookie是个基础的东西.是服务器发送到客户端,存储在客户端的一小段数据.可以存储一些配置信息,客户标识信息等.用户下次访问这个网站时,会把上次网站发来的cookie一同发送回去.cookie保存在 ...