Straightforward Java Solution


  • 0
    public class Solution {
        public boolean checkSubarraySum(int[] nums, int k) {
            if (nums == null || nums.length == 0) {
                return false;
            }
            int len = nums.length;
            int[] sums = new int[len];
            sums[0] = nums[0];
            for (int i = 1; i < len; i++) {
                sums[i] = sums[i - 1] + nums[i];
                if ((k == 0 && sums[i] == 0) || (k != 0 && sums[i] % k == 0)) {
                    return true;
                }
    
                for (int j = 0;  j < i - 1; j++) {
                    if (((sums[i] - sums[j]) == 0 && k == 0) || (k != 0 && ((sums[i] - sums[j]) % k == 0))) {
                        return true;
                    }
                }
    
            }
            return false;
        }
    }
    

Log in to reply
 

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