Subarray Sum

http://www.lintcode.com/en/problem/subarray-sum/

code

class Solution {
public:
    /**
     * @param nums: A list of integers
     * @return: A list of integers includes the index of the first number 
     *          and the index of the last number
     */
    vector<int> subarraySum(vector<int> nums){
        // write your code here
        vector<int> result;
        if(nums.size() == 0)
        {
            return result;
        }

        int sum = 0;
        unordered_map<int, int> hash;
        hash[0] = -1;
        int res = numeric_limits<int>::min();
        int minsum = 0;

        for(int i = 0; i<nums.size(); i++)
        {
            sum += nums[i];

            if(hash.find(sum) != hash.end())
            {
                result.push_back(hash[sum]+1);
                result.push_back(i);
                return result;
            }

            hash[sum] = i;

        }

        return result;
    }
};

\

results matching ""

    No results matching ""