leetcode 189. Rotate Array-旋转数组
来源:互联网 发布:eve欧服网络问题 编辑:程序博客网 时间:2024/06/06 03:23
原题链接:189. Rotate Array
【思路1-Java、Python】时间复杂度O(n),空间复杂度O(1)
以[1,2,3,4,5,6,7], k=3 为例,经过3次翻转,就能得到结果:
reverse(nums, 0, nums.length - 1); //[7,6,5,4,3,2,1] reverse(nums, 0, k - 1); //[5,6,7,4,3,2,1] reverse(nums, k, nums.length - 1); //[5,6,7,1,2,3,4]
public class Solution { public void rotate(int[] nums, int k) { k %= nums.length; reverse(nums, 0, nums.length - 1); reverse(nums, 0, k - 1); reverse(nums, k, nums.length - 1); } private void reverse(int[] nums, int m, int n) { while (m < n) { int temp = nums[m]; nums[m++] = nums[n]; nums[n--] = temp; } }}
33 / 33 test cases passed. Runtime: 1 ms Your runtime beats 11.70% of javasubmissions.
class Solution(object): def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: void Do not return anything, modify nums in-place instead. """ k %= len(nums) self.reverse(nums, 0, len(nums) - 1) self.reverse(nums, 0, k - 1) self.reverse(nums, k, len(nums) - 1) def reverse(self, nums, start, end) : while start < end : nums[start], nums[end] = nums[end], nums[start] start += 1 end -= 133 / 33 test cases passed. Runtime: 84 ms Your runtime beats 49.51% of pythonsubmissions.
【思路2-Java、Python】时间复杂度O(n),空间复杂度O(1)
以 k 为长度,循环移动 n 次,
以[1, 2, 3, 4, 5, 6, 7], k = 3为例,过程是这样的:
[1, 2, 3, 1, 5, 6, 7]
[1, 2, 3, 1, 5, 6, 4]
[1, 2, 7, 1, 5, 6, 4]
[1, 2, 7, 1, 5, 3, 4]
[1, 6, 7, 1, 5, 3, 4]
[1, 6, 7, 1, 2, 3, 4]
[5, 6, 7, 1, 2, 3, 4]
[5, 6, 7, 1, 2, 3, 4]
然而有时候有可能循环移动会出现死循环情况,所以要加上①处判断条件,以[1, 2, 3, 4, 5, 6], k = 2为例[1, 2, 1, 4, 5, 6]
[1, 2, 1, 4, 3, 6]
[5, 2, 1, 4, 3, 6]
[5, 2, 1, 4, 3, 6] //这时 distance = 0了,所以要调整
[5, 2, 1, 2, 3, 6]
[5, 2, 1, 2, 3, 4]
[5, 6, 1, 2, 3, 4] //同样 distance = 0
[5, 6, 1, 2, 3, 4]
public class Solution { public void rotate(int[] nums, int k) { for (int i=0, idx=0, cur=nums[0], distance=0, n=nums.length; i < n; i++) { idx = (idx + k) % n; int temp = cur; //交换 cur 和 nums[idx] cur = nums[idx]; nums[idx] = temp; distance = (distance + k) % n; //① if (distance == 0) { idx = (idx + 1) % n; cur = nums[idx]; } } }}33 / 33 test cases passed. Runtime: 2 ms Your runtime beats 2.72% of javasubmissions.
class Solution: # @param nums, a list of integer # @param k, num of steps # @return nothing, please modify the nums list in-place. def rotate(self, nums, k): n = len(nums) idx = 0 distance = 0 cur = nums[0] for x in range(n): idx = (idx + k) % n nums[idx], cur = cur, nums[idx] distance = (distance + k) % n if distance == 0: idx = (idx + 1) % n cur = nums[idx]33 / 33 test cases passed. Runtime: 88 ms Your runtime beats 40.12% of pythonsubmissions.
1 0
- LeetCode 189. Rotate Array(旋转数组)
- leetcode 189. Rotate Array-旋转数组
- leetcode 189. Rotate Array旋转数组
- leetcode:Rotate Array旋转数组
- 【LeetCode】Rotate Array 旋转数组
- Rotate Array 数组旋转 leetcode
- leetcode Rotate Array 旋转数组
- 189. Rotate Array 旋转数组
- 189. Rotate Array 旋转数组
- LeetCode Rotate Array(数组的旋转)
- LeetCode 189 Rotate Array(旋转数组)
- LeetCode 189 Rotate Array(旋转数组)
- Rotate Array数组旋转
- 189. Rotate Array (旋转数组)
- 字符串,数组旋转问题-leetcode 189 Rotate Array
- Rotate Array 数组的旋转
- [C++]Rotate Array 旋转数组
- Rotate Array 部分旋转数组
- openfire系列一---源码导入
- 关于 #ifdef __cplusplus extern "C" { #endif
- C语言中的关键字volatile
- TextView中ellipsize属性
- CPaintDC, CClientDC与CWindowDC的区别
- leetcode 189. Rotate Array-旋转数组
- 关于 重入
- 多态(C++版)
- android电池管理系统
- 实现WIN CE下截屏并且保存到文件
- List Leaves (25分)
- C#实验8Windows应用编程
- 【iOS小笔记】iOS工程报错及解决办法整理
- 【下载】《大教堂和市集》:开放源代码运动的《圣经》