Why always java.lang.ArrayIndexOutOfBoundsException


  • 0
    I
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            
            if(0<nums.length){
                Arrays.sort(nums);
                int sum = 0;
                int i = 0;
                while(sum<s){
                    i++;
                    int x = nums.length-i;
                    sum += nums[x];
                }
                return i;
            }
            else return 0;
        }
    }
    

    sum += nums[x];

    generate ArrayIndexOutOfBoundsException


  • 0
    Z

    because there is no upper bound for i, x could be negative.


Log in to reply
 

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