Java code O(1) extra space


  • 0
    C

    public class Solution{

    public void swap(int[] a, int i, int j)
    {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    
     public void rotate(int[] a, int k)
     {
        int N = a.length;
        k = k%N;
        int idx = N-1;
        int temp = N-1;
        for (int s = 0; s < N-1; s++)
        {
                idx = (idx+k)%N;            
                if(temp == idx)
                {
                    temp = temp-1;
                    idx = temp;
                    continue;
                }
                swap(a,temp,idx);           
        }
     }
    

    }


Log in to reply
 

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