my 1ms accepted java solution! but Expected Answer is Line 12: java.lang.ArithmeticException: / by zero??

  • 0

    The following is my code.
    When the test case is [], the output should return [], right?
    But the expected answer is: Line 12: java.lang.ArithmeticException: / by zero? Why???

    public class Solution {
        public void rotate(int[] nums, int k) {
            if(k==0||nums.length==0) return;
            k = k%(nums.length);
            swap(nums, nums.length-k, nums.length);
            swap(nums, 0, nums.length-k);
            swap(nums, 0, nums.length);
        public void swap(int[] arr, int i, int j){
                int tmp = arr[i];
                arr[i++] = arr[--j];
                arr[j] = tmp;

Log in to reply

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