Please help what is the problem


  • 0
    M
    public class Solution {
    public void rotate(int[] nums, int k) {
        if(nums.length==1) return;
       
        rotate(nums,nums.length-k,nums.length-1);
        rotate(nums,0,nums.length-k-1);
        rotate(nums,0,nums.length-1);
    }
    public void swap(int[] nums,int a,int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
    
    public void rotate(int[] nums,int start,int end){
        for(int i = start;i<=(start+end)/2;i++){
            swap(nums,i,(start+end)-i);
        }
    }
    

    }

     Line 11: java.lang.ArrayIndexOutOfBoundsException: -1
        Last executed input: [1,2], 2

  • 0
    C

    Its because you are sending -1 to the swap method as parameter b.

    rotate(nums,0,nums.Length -k-1);
    // nums.length = 2
    // k = 2
    
    nums[b] gives out of index error cuz b is -1

  • 0
    M

    Thanks i was abel to fix the bug!


Log in to reply
 

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