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 ...
随机推荐
- linux、windows下导入、导出mysql数据库命令
一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): 1.导出数据和表结构:[不是mysql里的命令]mysqldump -u用户名 -p密码 数据库名 > 数据 ...
- Vi编辑器下使用分屏显示【已自己验证所有】
:new 水平分割出一个新窗口 :vnew,:vne 垂直分割出一个新窗口 :new+文件路径/文件名; 在新的水平分屏中 载入/新建 文件.[文件存在则载入,不存在则在指定的路径新建,下同] :vn ...
- Mysql ERROR 1064 (42000)
创建了一个表: +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null ...
- [SAP ABAP开发技术总结]BAPI调用
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 优秀的Markdown编辑器MarkdownPad2免费版使用全功能
MarkdownPad,一款不错的Markdown编辑器,本人一直在用,具备所有Markdown的基本语法外支持一些特别的扩展,比如表格等. MarkdownPad分为免费版和收费版,区别是免费版不支 ...
- C# 线程(二):关于线程的相关概念
From : http://kb.cnblogs.com/page/42528/ 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又 ...
- 四十条测试你是不是合格的PHP程序员
四十条测试你是否合格的PHP程序员,不官方,也不权威,但很给力.超过三条就不合格了.超过五条就得好好反省下自己的不足了. 1. 不会利用如phpDoc这样的工具来恰当地注释你的代码 2. 对优秀的集成 ...
- mysql概要(三)having
1.运算符 2.模糊查询 3.where 后的判断基于表(表的直接内容),而不是基于结果(运算之后的别名)如: 可改成在where后再次重新计算判断: 或者使用having对结果判断: having多 ...
- Spring 读书笔记-----使用Spring容器(一)
pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...
- 查看package编译时的错误信息及重新编译
开发时,一般都是使用PL/SQL工具进行开发,查看编译错误及重新编译都很简单,但是一般的生产环境是不允许连接外界工具的,只能在命令行中进行重新编译及查看,今天我就遇到了这个问题,现在总结如下: 1.获 ...