TypechoJoeTheme

IT技术分享

统计

[LeetCode 189] Rotate Array [Java]

2017-12-22
/
0 评论
/
859 阅读
/
正在检测是否收录...
12/22

1. Description

Rotate an array of n elements to the right by k steps.

2. 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].

3. Code

import java.util.Arrays;

public class LeetCode0189 {
    public void rotate(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k < 0) {
            return;
        }

        k %= nums.length;

        reverse(nums, 0, nums.length - 1 - k);
        reverse(nums, nums.length - k, nums.length - 1);
        reverse(nums, 0, nums.length - 1);

    }

    private void reverse(int[] array, int start, int end) {
        int mid = (end - start) >> 1;
        for (int i = 0; i <= mid; i++) {
            if (array[start + i] == array[end - i]) {
                continue;
            }
            array[start + i] ^= array[end - i];
            array[end - i] ^= array[start + i];
            array[start + i] ^= array[end - i];
        }
    }

    public static void main(String[] args) {
        LeetCode0189 leetcode = new LeetCode0189();
        int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        leetcode.rotate(nums, 3);
        System.out.println(Arrays.toString(nums));
    }
}
Array
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/1305/(转载时请注明本文出处及文章链接)