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 ...
随机推荐
- sequenza细胞纯度计算
安装sequenza bam文件要放在前面,否侧会-f命令可能识别错误 samtools mpileup a.bam -f hg19.fasta -Q 20 |gzip > normal.pil ...
- Mybatis Generator(定制化)代码生成器
1.使用Mapper专用的MyBatis Generator插件 通用Mapper在1.0.0版本的时候增加了MyBatis Generator(以下简称MBG)插件,使用该插件可以很方便的生成实体类 ...
- 关于inline-block 元素之间为何会产生间隔
关于inline-block 元素之间为何会产生间隔 现象: <body> <input type="text"> <input type=" ...
- java实现excel与mysql的导入导出
注意:编码前先导入poi相关jar包 1 /** * 读excel 到list * * @param file excel file * @param fields 字段数组 * @return * ...
- Python学习(17)异常处理
目录 Python 异常处理 Python 标准异常 异常处理 使用except而不带任何异常类型 使用except而带多种异常类型 try-finally 语句 异常参数 异常的参数 用户自定义参数 ...
- mysql 性能问题
1.场景,模拟一天的数据,每个10秒,遍历1000个设备,每个设备模拟一个实时数据,总的数据量为:24*60*60/10*1000 = 864万条记录.2.采用策略,对时间分段,拼接sql语句查询,对 ...
- [转]Java代码(性能)优化总结
前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用, ...
- Java可变参数 & Python可变参数 & Scala可变参数
Java 可变参数的特点: (1).只能出现在参数列表的最后: (2)....位于变量类型和变量名之间,前后有无空格都可以: (3).调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体 ...
- go中间的&和*
package main import "fmt" func main() { var a int = 1 var b *int = &a var c **int = &a ...
- Html_在线客服静态网页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...