@@ -0,0 +1,1844 @@
+//  Copyright 2019 The KeepTry Open Source Project%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A//%0A%0Apackage array_presum;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**%0A * Given an array of integers and an integer k, you need to find the total number of continuous%0A * subarrays whose sum equals to k.%0A *%0A * %3Cpre%3E%0A *   Example 1:%0A *   Input:nums = %5B1,1,1%5D, k = 2%0A *   Output:  2%0A *%0A *   Note:%0A *   The length of the array is in range %5B1, 20,000%5D.%0A *   The range of numbers in the array is %5B-1000, 1000%5D%0A *   and the range of the integer k is %5B-1e7, 1e7%5D.%0A */%0Apublic class Leetcode560SubarraySumEqualsK %7B%0A    // O(n)%0A    public int subarraySum(int%5B%5D nums, int k) %7B%0A        // map Sum To its Counts%0A        Map%3CInteger, Integer%3E sn = new HashMap();%0A        int s = 0, r = 0;%0A        sn.put(s, 1); // empty array%0A        for (int i : nums) %7B%0A            s += i;%0A            r += sn.getOrDefault(s - k, 0);%0A            sn.put(s, sn.getOrDefault(s, 0) + 1);%0A        %7D%0A        return r;%0A    %7D%0A%0A    public int subarraySum2(int%5B%5D nums, int k) %7B%0A        // map Sum To its Counts%0A        Map%3CInteger, Integer%3E sn = new HashMap();%0A        int s = 0, r = 0;%0A        for (int i : nums) %7B%0A            sn.put(s, sn.getOrDefault(s, 0) + 1);%0A            s += i;%0A            r += sn.getOrDefault(s - k, 0);%0A        %7D%0A        return r;%0A    %7D%0A%7D%0A

Presum Leetcode 560. Subarray Sum Equals K