LeetCode 189 Rotate Array (思维)
来源:互联网 发布:手机淘宝店铺怎么激活 编辑:程序博客网 时间:2024/06/05 22:33
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.
Hint:
Could you do it in-place with O(1) extra space?
public class Solution { public void swap(int[] nums, int i, int j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } public void reverse(int[] nums, int l, int r) { while (l < r) { swap(nums, l, r); l ++; r --; } } public void rotate(int[] nums, int k) { int n = nums.length; if (n < 2) { return; } k %= n; reverse(nums, 0, n - 1); reverse(nums, 0, k - 1); reverse(nums, k, n - 1); }}
题目分析:交换三次,空间O(1),时间O(n)
阅读全文
0 0
- LeetCode 189 Rotate Array (思维)
- [LeetCode][189][Rotate Array]
- Leetcode 189: Rotate Array
- leetCode #189 Rotate Array
- LeetCode #189 Rotate Array
- LeetCode(189) Rotate Array
- [LeetCode 189]Rotate Array
- LeetCode #189 Rotate Array
- LeetCode 189 Rotate Array
- leetcode-189 Rotate Array
- leetcode 189 Rotate Array
- leetcode-189 Rotate Array
- Leetcode[189]-Rotate Array
- leetcode[189]:Rotate Array
- leetcode 189: Rotate Array
- [Leetcode]#189 Rotate Array
- leetcode 189 Rotate Array
- 【LeetCode】189Rotate Array
- python 爬取学信网登录页面
- 微软创立全新人工智能实验室,与DeepMind、OpenAI同台竞技
- 线程
- 网络基础之iso,osi七层模型
- sqlServer里面查询一张表里面有没有自增字段
- LeetCode 189 Rotate Array (思维)
- Bootstrap轮播图不兼容IE的问题
- Java注解全解
- 【C#】集合已修改;可能无法执行枚举操作。
- centOS7设置静态ip后无法上网的解决
- DxO OpticsPro 11.4.2.68 for Mac 简体中文全汉化版 送4部教程 完美支持 OS X 10.12 系统
- mtparts & dts partition
- 基于Dragonboard 410c的燃气烟雾检测器(一)
- queue顺序C++