189. Rotate Array
来源:互联网 发布:linux 修改网络地址 编辑:程序博客网 时间:2024/06/06 06:48
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]
.
用三个反转,空间复杂度为O(1)
public class Solution { public void rotate(int[] nums, int k) { k%=nums.length; reverse(nums,0,nums.length-1-k); reverse(nums,nums.length-k,nums.length-1); reverse(nums,0,nums.length-1); } public void reverse(int[] nums,int start,int end){ int length=end-start+1; for(int i=0;i<length/2;i++){ int temp=nums[end-i]; nums[end-i]=nums[start+i]; nums[start+i]=temp; } }}
阅读全文
0 0
- Leetcode - Array - 189. Rotate Array
- 189. Rotate Array leetcode(array)
- LeetCode-Array-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
- 189. Rotate Array LeetCode
- 189. Rotate Array
- [LeetCode]189. Rotate Array
- 189. Rotate Array
- leetcode-189.-Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- 寻找最长不重复子串
- 【AlphaGo】【论文阅读】
- 用java语言画一个可视化日历
- leofs-storage(二)
- 你应该知道的Gradle知识
- 189. Rotate Array
- i++和++i
- 数据库索引
- Batch Normalization笔记
- Day 9 linux基础 samba及Nginx服务
- MapReduce实例----单表关联
- 设计模式
- Ubuntu16.04安装x11VNC远程桌面
- 理解ROS话题(六)