轮转数组 (Rotate Array)

 

思路:

// @Title: 轮转数组 (Rotate Array)
// @Author: qisiii
// @Date: 2019-05-07 18:31:32
// @Runtime: 1 ms
// @Memory: 37.8 MB
// @comment: 
// @flag: 
class Solution {
    public void rotate(int[] nums, int k) {
       
        int len = nums.length-1;
        if(k >= nums.length){
            k=k-nums.length;
        }
        if(k < nums.length){
             reverseArray(0,len,nums);
            reverseArray(0,k-1,nums);
            reverseArray(k,len,nums);
           
        }
        
    }
    public static void reverseArray(int start, int end, int[] nums){
        int temp = 0;
        while(start < end){
            temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}

思路:

// @Title: 轮转数组 (Rotate Array)
// @Author: qisiii
// @Date: 2021-08-16 13:13:37
// @Runtime: 0 ms
// @Memory: 55 MB
// @comment: 
// @flag: 
class Solution {
    public void rotate(int[] nums, int k) {

                int len = nums.length-1;
                if(k >= nums.length){
                    k=k%nums.length;
                }
                if(k < nums.length){
                    reverseArray(0,len,nums);
                    reverseArray(0,k-1,nums);
                    reverseArray(k,len,nums);

                }

            }
            public static void reverseArray(int start, int end, int[] nums) {
                int temp = 0;
                while (start < end) {
                    temp = nums[start];
                    nums[start] = nums[end];
                    nums[end] = temp;
                    start++;
                    end--;
                }
            }
}

+++ title = “轮转数组 (Rotate Array)” draft = false +++

思路:多次反转

// @Title: 轮转数组 (Rotate Array)
// @Author: qisiii
// @Date: 2022-03-10 13:14:54
// @Runtime: 1 ms
// @Memory: 57.2 MB
// @comment: 多次反转
// @flag: GREEN
class Solution {
    public void rotate(int[] nums, int k) {
        if(k==0){
            return;
        }
        k=k%nums.length;
        reserver(nums,0,nums.length-1);
        reserver(nums,0,k-1);
        reserver(nums,k,nums.length-1);
    }

    public void reserver(int[] nums,int left,int right){
        while(left<right){
            int temp=nums[left];
            nums[left]=nums[right];
            nums[right]=temp;
            left++;right--;
        }
    }
}

思路:

// @Title: 轮转数组 (Rotate Array)
// @Author: qisiii
// @Date: 2021-08-16 14:58:40
// @Runtime: 0 ms
// @Memory: 55.5 MB
// @comment: 
// @flag: 
class Solution {
    public void rotate(int[] nums, int k) {

                int len = nums.length-1;
                if(k >= nums.length){
                    k=k%nums.length;
                }
                if(k < nums.length){
                    reverseArray(0,len,nums);
                    reverseArray(0,k-1,nums);
                    reverseArray(k,len,nums);

                }

            }
            public static void reverseArray(int start, int end, int[] nums) {
                int temp = 0;
                while (start < end) {
                    temp = nums[start];
                    nums[start] = nums[end];
                    nums[end] = temp;
                    start++;
                    end--;
                }
            }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18