Rotate Array--Leetcode(Java)
来源:互联网 发布:背单词软件排行 编辑:程序博客网 时间:2024/05/01 21:51
public void rotate(int[] nums, int k) { int n = nums.length; k %=n; reverse(nums, 0, n-k-1); reverse(nums, n-k, nums.length-1); reverse(nums, 0, nums.length-1); } private void reverse(int[] nums, int start, int end){ while(start<end){ int tmp=nums[start]; nums[start]=nums[end]; nums[end]=tmp; start++; end--; } }
超简单的题,竟然做了超久,不停的有bug...
主要出现的bug:
1. k要考虑比list长度大,所以有mod
2. 由于自己例子枚举好,index错了自己看了半天,有时候可以换换例子。
思路很简单,就是做3次reverse. 见上面code
0 0
- Rotate Array - LeetCode - Java
- Rotate Array--Leetcode(Java)
- Leetcode: Rotate Array (Java)
- 【leetcode】Rotate Array【java】
- LeetCode|Rotate Array-java
- leetcode:Rotate Array 【Java】
- LeetCode(189) Rotate Array(Java)
- LeetCode Rotate Array Java版本
- leetcode oj java - Rotate Array
- leetcode-java-189. Rotate Array
- [leetcode][array] Rotate Array
- Rotate Array---leetcode 我的java题解
- [Leetcode]Rotate Array java(三种)
- Leetcode:189. Rotate Array(JAVA)
- LeetCode[Array]: Rotate Image
- LeetCode:Rotate Array
- [LeetCode] Rotate Array
- *LeetCode-Rotate Array
- php全局变量和正则表达式错误解决方案
- 希尔排序算法
- 成为一个优秀的软件开发工程师应该具备的能力
- 选择排序
- 分拆数组技巧应用
- Rotate Array--Leetcode(Java)
- 求三角形面积
- String创建对象
- uva_537
- SQL基本语法
- 例题10-22 统计问题 UVa1640
- wxWidgets与其他工具库的比较
- 两点间距离(2D , 3D)
- 计算阶乘n!末尾所含的0的个数