*LeetCode-Rotate Array
来源:互联网 发布:安卓刷机软件哪个最好 编辑:程序博客网 时间:2024/04/30 23:10
1.最初新建了一个数组,顺序对了之后想直接让原数组等于新数组,但是不可以,因为java的指针问题,回去看书。
2.将新数组一个一个assign给了原数组,时间虽然是n的 但是空间也是n。这个题时间只能是n 但是空间可以1
3.看了答案后,发现只要自己写一个reverse,整个数组reverse三次就可以了。
注意:k要先%length之后再用。要判断nums数组是否为空。
public class Solution { public void rotate(int[] nums, int k) { if ( k <= 0 || nums.length == 0 || nums == null ) return; k = k%nums.length; reverse(nums, 0, nums.length - 1 ); reverse(nums, 0, k - 1 ); reverse(nums, k, nums.length - 1 ); return; } public void reverse( int[] nums, int start, int end){ while (start < end){ int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start ++; end --; } }}
0 0
- *LeetCode-Rotate Array
- [leetcode][array] Rotate Array
- LeetCode[Array]: Rotate Image
- LeetCode:Rotate Array
- [LeetCode] Rotate Array
- [LeetCode]189.Rotate Array
- Rotate Array - LeetCode - Java
- Rotate Array--Leetcode(Java)
- Leetcode--Rotate Array
- LeetCode: Rotate Array
- LeetCode Rotate Array
- Rotate Array(leetcode)
- leetcode Rotate Array
- Leetcode: Rotate Array
- [Leetcode]11 Rotate Array
- [LeetCode][189][Rotate Array]
- Leetcode:Rotate Array
- LeetCode: Rotate Array
- Canvas实战---模仿GOOGLE浮动小球效果
- opencv 霍夫变换检测直线
- linux学习基础杂记
- 2015年,除了阿里和腾讯,我们还能不能聊点别的?
- 工厂模式
- *LeetCode-Rotate Array
- 3Swift\OC\Java中字符串的比较
- Java异常处理新特性
- Android项目工程目录详解
- SpringSide session短时间失效
- java 枚举的使用示例
- 数据库查询select with子句
- 如何在apache下实现404重定向
- bzoj1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 计算几何 凸包