share my java solution, short!


  • 0
    T
    public class Solution {
        public int subarraySum(int[] nums, int k) {
              if(nums==null || nums.length<1){
                      return 0;  
              }
              Map<Integer,Integer> map=new HashMap<>();
              map.put(0,1);
              int sum=0;
              int res=0;
              for(int i=0;i<nums.length;i++){
                  sum+=nums[i];
                  
                  res+=map.getOrDefault(sum-k,0);
                  
                  map.put(sum,map.getOrDefault(sum,0)+1);
              }
              
              return res;
        }
    }
    

Log in to reply
 

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