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 ...
随机推荐
- CSS在线字体库,外部字体的引用方法
目录: 1:CSS家族五大字体 2:360和谷歌外部字体引用方法 3:谷歌外部字体引用方法详解 4:@font-face用法详解 一: {font-family:serif,sans-serif,fa ...
- Servlet&jsp基础:第四部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- win7删除IE图标方法-----(注册表找不到对应的IE项)
删除个IE图标费了半天事,硬是找不到网上说的那个 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desk ...
- HDU 5724 Chess(国际象棋)
HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- 新浪博客地址 http://blog.sina.com.cn/u/2145079955
原来 新浪博客地址 http://blog.sina.com.cn/u/2145079955
- 一致性 hash 算法( consistent hashing )
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在cache 系统中应用越来越广泛: 1 基 ...
- Hibernate 对象的三种状态
hibernate对象的三种状态: (一) 瞬时(临时)状态: 对象被创建时的状态,数据库里面没有与之对应的记录! (二) 持久状态: 处于session的管理中,并且数据库里面存在与之对应的 ...
- bootstrap学习笔记<二>(标题,段落样式)
标题.样式:class="h1"~class="h6" bootstrap中重新定义了h1~h6标签,具体差别如下: 在bootstrap中其他任何标签使用cl ...
- scala集合
优先使用不可变集合.不可变集合适用于大多数情况,让程序易于理解和推断,因为它们是引用透明的( referentially transparent )因此缺省也是线程安全的. 使用可变集合时,明确地引用 ...