very simple java solustion (no hashmap)


  • 0
    G

    class Solution {
    public int subarraySum(int[] nums, int k) {
    int[] sum = new int[nums.length + 1];
    int res = 0;
    sum[0] = 0;
    for(int i= 0 ;i < nums.length;i++) sum[i+1] = nums[i] + sum[i];
    for(int i = 0;i <= nums.length;i++) {
    for(int j = 0;j <i;j++ ) {
    if(sum[i] - sum[j] == k) res++;
    }
    }
    return res;
    }
    }


Log in to reply
 

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