if(k>0 && (nums.length-k)>1) return true; beat 98% people


  • 0
    Y

    just add one line ,it worked well ,
    public static boolean checkSubarraySum(int[] nums, int k) {
    if(k>0 && (nums.length-k)>1) return true;
    Map<Integer, Integer> map = new HashMap<Integer, Integer>(){{put(0,-1);}};;
    int runningSum = 0;
    for (int i=0;i<nums.length;i++) {
    runningSum += nums[i];
    if (k != 0) runningSum %= k;
    Integer prev = map.get(runningSum);
    if (prev != null) {
    if (i - prev > 1) return true;
    }
    else map.put(runningSum, i);
    }
    return false;
    }


  • 1
    Y

    yes ,you are right !!!


  • 1
    1

    yes ,you are right !!!


  • 0
    M

    Try this:
    [0,1,0,3,0,4,0,4,0]
    5

    Missing test case application submitted.


Log in to reply
 

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