Java One of Solution,Easy to Understand


  • 1
    public class Solution {
        public void rotate(int[] nums, int k) {
            int n = nums.length;
            
            if(n == 0) return;
            
            k = k % n;
            
            if(k == 0) return;
            
    		int[] rux = new int[n];
    		
    		for(int i = 0,j = n - k; j < n; i++,j++){
    			rux[i] = nums[j];
    		}
    		for(int i = k, j = 0; j < n - k ;i++,j++){
    			rux[i] = nums[j];
    		}
    		for(int i = 0; i < n; i++){
    			nums[i] = rux[i];
    		}
        }
    }
    

Log in to reply
 

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