Accepted Java Solution


  • 0
    B

    public class Solution {

    public void rotate(int[] nums, int k) {
    
          int n = nums.length;
          int rotate = k % n;
        
        
       
      if(rotate != 0 && n != 0 && n != 1){
        
        int[] newArray = new int[n];
        
          for(int i = 0; i < nums.length - rotate; i++){
              newArray[i] = nums[i];
          }
          
          for(int j = 0; j < rotate; j++){
              nums[j] = nums[nums.length - rotate + j];
          }
          
          for(int m = 0; m < nums.length - rotate; m++){
              nums[rotate + m] = newArray[m];
          }
       
      }  
        
    }
    

    }


Log in to reply
 

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