zoukankan      html  css  js  c++  java
  • 189. Rotate Array

    题目:

    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.

    [show hint]

    Related problem: Reverse Words in a String II

    Credits:
    Special thanks to @Freezen for adding this problem and creating all test cases.

    Hide Tags
     Array
     

    链接:  http://leetcode.com/problems/rotate-array/

    题解:

    数组移动问题。可以参考<编程珠玑>里利用三次reverse达成数组向左移动的方法。向右移动k step相当于向左移动数组长度nums.length - k。 要注意提前取摸,使k成为一个合理的值。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public void rotate(int[] nums, int k) {
            k %= nums.length;
            k = nums.length - k;
            reverse(nums, 0, k - 1);
            reverse(nums, k, nums.length - 1);
            reverse(nums, 0, nums.length - 1);
        }
        
        private void swap(int[] nums, int l, int r){
            int temp = nums[l];
            nums[l] = nums[r];
            nums[r] = temp;
        }
        
        private void reverse(int[] nums, int l, int r){
            while(l <= r)
                swap(nums, l ++, r --);
        }
    }

    Update:

    依然是三步反转法,先反转整个数组,再翻转0 ~ k - 1, 最后翻转 k ~ n - 1

    public class Solution {
        public void rotate(int[] nums, int k) {
            if(nums == null || nums.length == 0)
                return;
            int n = nums.length;
            k %= n;
            
            reverse(nums, 0, n - 1);
            reverse(nums, 0, k - 1);
            reverse(nums, k, n - 1);
        }
        
        private void reverse(int[] nums, int lo, int hi) {
            while(lo < hi)
                swap(nums, lo++, hi--);
        }
        
        private void swap(int[] nums, int lo, int hi) {
            int tmp = nums[lo];
            nums[lo] = nums[hi];
            nums[hi] = tmp;
        }
    }

    二刷: 

    三步反转法.

    Java:

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public void rotate(int[] nums, int k) {
            if (nums == null || nums.length == 0 || k == 0) {
                return;
            }       
            int len = nums.length;
            k %= len;
            reverse(nums, 0, len - 1);
            reverse(nums, 0, k - 1);
            reverse(nums, k, len - 1);
        }
        
        private void swap(int[] nums, int i, int j) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        } 
        
        private void reverse(int[] nums, int i, int j) {
            while (i < j) {
                swap(nums, i++, j--);
            }
        }
    }

    三刷:

    Java:

    public class Solution {
        public void rotate(int[] nums, int k) {
            if (nums == null || nums.length == 0 || k <= 0) return;
            int len = nums.length;
            k %= len;
            reverse(nums, 0, len - 1);
            reverse(nums, 0, k - 1);
            reverse(nums, k, len - 1);
        }
        
        private void reverse(int[] nums, int i, int j) {
            while (i < j) {
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
                i++;
                j--;
            }
        }
    }

    Reference:

  • 相关阅读:
    利用递归分割(Split)字符串
    SQL Server2012 T-SQL基础教程--读书笔记(1-4章)
    kindeditor编辑器的使用
    echarts绘制四川地图
    Windows下搭建PHP开发环境(Apache+PHP+MySQL)+调试工具Xdebug的配置
    给搜索关键字添加高亮,加以颜色区分
    SQL 生成6位随机数并MD5加密输出
    微信小程序登录 .net 后端实现
    钉钉小程序http post 请求
    浅谈Web Api配合SignalR的跨域支持
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4491660.html
Copyright © 2011-2022 走看看