short java solution beats 98.01%


  • 0
    M

    public class Solution {
    public boolean checkSubarraySum(int[] nums, int k) {
    Set<Integer> sums = new HashSet<>();
    sums.add(0);
    nums[0]= k==0? nums[0]:nums[0]%k;
    for(int i=1; i<nums.length; i++){
    nums[i] = k==0? (nums[i]+nums[i-1]):(nums[i]+nums[i-1])%k;
    if(sums.contains(nums[i])) return true;
    sums.add(nums[i-1]);
    }
    return false;
    }
    }


Log in to reply
 

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