Easy to read Java solution


  • 263
    D

    I really don't like those something little line solutions as they are incredibly hard to read. Below is my solution.

    public void rotate(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }
    
    public void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }

  • -27
    L

    I have a similar solution. Though long but very easy to understand...

    class Solution {
    public:
        void rotate(vector<int>& nums, int k) {
            if(!nums.size()) return;
            k %= nums.size();
            // well, it depends on left rotate or right rotate
            // in this case, its right rotate so need to do a simple math
            k = nums.size()-k;  
            int temp;
            // reflect [0,k-1], [k,size()-1]
            // then reflect the whole array
            // e.g. [1,2,|3,4] --> [2,1,|4,3] --> [3,4,1,2]
            int l = 0, r = k-1;
            while(l<r) {
                temp = nums[l];
                nums[l] = nums[r];
                nums[r] = temp;
                l++,r--;
            }
            l = k, r = nums.size()-1;
            while(l<r) {
                temp = nums[l];
                nums[l] = nums[r];
                nums[r] = temp;
                l++,r--;
            }
            l = 0, r = nums.size()-1;
            while(l<r) {
                temp = nums[l];
                nums[l] = nums[r];
                nums[r] = temp;
                l++,r--;
            }
        }
    };

  • 1
    Z

    very good solution, thanks


  • 0

    Clear and make sense. Thanks!


  • 0
    S

    @danny6514 very concise solution!!! but could you explain the main idea behind it? thank you so much.


  • 2
    A

    People are not so nice...


  • 2
    K

    poor guy....


  • 1
    This post is deleted!

  • 2
    E
    public class Solution {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        if(k==n) return;
        if(k==0) return;
        if(n<=1) return;
        
        int start = 0;
        int swapPos = k%n;
        int temp;
        for(int step=0;step<n;step++){
            if(swapPos==start){
                start++;
                swapPos = (start+k)%n;
                continue;
            } 
            temp = nums[start];
            nums[start] = nums[swapPos];
            nums[swapPos] = temp;
            swapPos = (swapPos+k)%n;
        }
    }
    

    }


  • 4
    Y

    It's legit and it's amazing. but I can't figure out why it's legit.


  • 0
    A

    can someone explain this solution .


  • 30
    A

    let a= [1,2,3,4,5,6,7]
    k = 3.

    1. we have to first reverse the whole array by swapping first element with the last one and so on..
      you will get[7,6,5,4,3,2,1]

    2. reverse the elements from 0 to k-1
      reverse the elements 7,6,5
      you will get [5,6,7,4,3,2,1]

    3. reverse the elements from k to n-1
      reverse the elements 4,3,2,1
      you will get[5,6,7,1,2,3,4]


  • 0
    F

    this is so smart


  • 0
    H

    this problem is from the book of "beauty of programming"


  • 0
    C

    elegant solution, thank you!


  • 1
    S

    I don't understand the role of k%=nums.length. Doesn't it change the value of k? I tried without it and i get index out of bound in the reverse function.


  • 5
    J

    @sonakshi4 This is because that sometimes, k is larger than the length of array. For example nums = [1, 2, 3, 4, 5], k = 12, this means we only need to rotate the last two numbers. k = k % nums.length = 2;


  • 0
    S

    @junpitt I didnt think of that possibility. Thank you for replying :)


  • 1

    @ycai13 said in Easy to read Java solution:

    legit

    I am going to explain this with a simple example. Say with n = 7 and k = 3, the array = [1,2,3,4,5,6,7].

    1. Reverse the whole array, so array = [7,6,5,4,3,2,1]
    2. Reverse the first k elements, so array = [5,6,7,4,3,2,1]
    3. Reverse [k+1, n] elements, so array = [5,6,7,1,2,3,4]

  • 1
    R

    niubi...thx~~~~~


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.