Rotate Array

原创 2016年08月30日 16:36:41

Problem

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]. How many different ways do you know to solve this problem?

这道题是旋转数组。大致思路如下:
1.复制数组,最简单的方式。
2.采用类似冒泡排序的方式,每次将最右边一个移到最前面。
3.采用旋转逆序的方式。

talk is cheap,show me the code!

public class RotateArray {

    public static void printArray(int[] array) {
        for(int i=0; i<array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    //方法一:复制数组,时间复杂度与空间复杂度均为O(n)
    public static void rotate(int[] nums, int k) {
        if(k > nums.length) {
            k%= nums.length;
        }

        int[] result = new int[nums.length];

        for(int i=0; i<k; i++) {
            result[i] = nums[nums.length-k+i];
        }

        int j = 0;
        for(int i=k; i<nums.length; i++) {
            result[i] = nums[j];
            j++;
        }

        System.arraycopy(result,0,nums,0,nums.length);
    }

    //方法二:类似于冒泡排序,时间复杂度O(nk),空间复杂度O(1)
    public static void bubble_rotate(int[] nums, int k) {
        if(nums == null || k < 0) {
            throw new IllegalArgumentException("Illegal argument!");
        }

        for(int i=0; i<k; i++) {
            for(int j=nums.length-1; j>0; j--) {
                int tmp = nums[j];
                nums[j] = nums[j-1];
                nums[j-1] = tmp;
            }
        }
    }

    //方法三:逆序的方式,时间复杂度O(n),空间复杂度O(1)
    public static void reverse_rotate(int[] nums, int k) {
        if(nums == null || k < 0) {
            throw new IllegalArgumentException("Illegal argument!");
        }

        reverse_array(nums,0,k-1);
        reverse_array(nums,k,nums.length-1);
        reverse_array(nums,0,nums.length-1);
    }

    public static void reverse_array(int[] array, int left, int right) {
        if (array == null || array.length == 0 || left < 0 || right < 0) {
            throw new IllegalArgumentException("Illegal argument!");
        }

        while(left < right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        reverse_rotate(nums,3);
        printArray(nums);
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Array(4) -- Rotate Array

Rotate Array

Rotate Array

ProblemRotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Rotate Array

最直觉的解法:缓存需要移动到原数组开头的元素。 注意当循环移动n位时,相当于没有移动,所以k%n即可。 class Solution { public: void rotate(int num...

Rotate Array

leetcode中的array的rotate问题

Rotate Array

题目详情:https://leetcode.com/problems/rotate-array/description/ 自己写的代码:class Solution(object): def...

Rotate Array

题目名称 Rotate Array—LeetCode链接描述 Rotate an array of n elements to the right by k steps.For example, ...

Rotate Array

题目大意: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k =...

Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr...

rotate array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array...

leetcode--Rotate Array

/*  * 需求:旋转数组  * Rotate an array of n elements to the right by k steps.  */ 在自己的eclipse上写的...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)