博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Rotate Array
阅读量:5880 次
发布时间:2019-06-19

本文共 2425 字,大约阅读时间需要 8 分钟。

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2Output: [3,99,-1,-100]Explanation: rotate 1 steps to the right: [99,-1,-100,3]rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

 

 

 

Solution 1 - Intermediate Array

Space is O(n) and time is O(n). 

class Solution {    public void rotate(int[] nums, int k) {        if(nums == null){            return;        }        if(k >= nums.length){            k = k % nums.length;        }        int[] temp = new int[nums.length];        int count=0;        for(int i=nums.length-k; i

 

Solution 2 - Bubble Rotate

O(1) space,O(n*k) time

class Solution {    public void rotate(int[] nums, int k) {        if(nums == null){            return;        }        if(k >= nums.length){            k = k % nums.length;        }        for (int i=0; i
0; j--){ int temp = nums[j-1]; nums[j-1] = nums[j]; nums[j] = temp; } } }}

 

Solution 3 - Reversal

Can we do this in O(1) space and in O(n) time? The following solution does.

Assuming we are given {1,2,3,4,5,6} and order 2. The basic idea is:

1. Divide the array two parts: 1,2,3,4 and 5, 62. Reverse first part: 4,3,2,1,5,63. Reverse second part: 4,3,2,1,6,54. Reverse the whole array: 5,6,1,2,3,4
class Solution {    public void rotate(int[] nums, int k) {        if(nums == null){            return;        }        if(k >= nums.length){            k = k % nums.length;        }        reverse(nums, 0, nums.length-k-1);        reverse(nums, nums.length-k, nums.length-1);        reverse(nums, 0, nums.length-1);    }        public void reverse(int[] nums, int left, int right){        if(nums == null || nums.length == 1){            return;        }        while(left < right){            int temp = nums[left];            nums[left] = nums[right];            nums[right] = temp;            left++;            right--;        }    }}

 

转载于:https://www.cnblogs.com/incrediblechangshuo/p/9062422.html

你可能感兴趣的文章
WPF 实现窗体拖动
查看>>
来自维基百科程序员Brandon Harris
查看>>
NULL不是数值
查看>>
CentOS 5 全功能WWW服务器搭建全教程
查看>>
30个优秀的后台管理界面设计案例分享
查看>>
scala111
查看>>
模块化服务规范——OSGI
查看>>
劣质代码评析——猜数字问题(上)
查看>>
纸上谈兵: 栈 (stack)
查看>>
Windows phone8 基础篇(三) 常用控件开发
查看>>
Oracle学习笔记之五,Oracle 11g的PL/SQL入门
查看>>
大叔手记(3):Windows Silverlight/Phone7/Mango开发学习系列教程
查看>>
考拉消息中心消息盒子处理重构(策略模式)
查看>>
so easy 前端实现多语言
查看>>
【追光者系列】HikariCP源码分析之ConcurrentBag&J.U.C SynchronousQueue、CopyOnWriteArrayList...
查看>>
在navicat中如何新建连接数据库
查看>>
canvas系列教程05-柱状图项目3
查看>>
css绘制几何图形
查看>>
HTML标签
查看>>
理解JS中的Event Loop机制
查看>>