My 10ms non-recursion answer


  • 0
    X
    int gcd(int a, int b){  
    
    while(b^=a^=b^=a%=b);  
         return a; 
    
    }
    
    void rotate(int nums[], int n, int k){
    
       int i, _i, j, _j, t;
    	k %= n;
    	if (k == 0) return;
    	for ( i = 0, _i = gcd(n, k); i < _i; ++i) {
    		 _j = j = i;
    		do {
    			j = (j + k) % n;
    			 t = nums[j];
    			nums[j] = nums[_j];
    			nums[_j] = t;
    			
    		} while (j != i);
    	}
    }

  • 0
    S

    can you please explain,what is the gcd function meant for & what exactly are you doing


Log in to reply
 

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