Subarray Sum

Subarray Sum

Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

暴力解法

public class Solution {
    /**
     * @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
     */
    public List<Integer> subarraySum(int[] nums) {
        List<Integer> result = new ArrayList<>();
        if (nums == null || nums.length == 0){
            return result;
        }
        
        int sum = 0;
        
        // 从每一个数开始找subarray
        for (int i = 0; i < nums.length; i++){
            
            // 单个的subarray和为0 return
            if (nums[i] == 0){
                result.add(i);
                result.add(i);
                return result;
            }
            
            sum = nums[i];
            
            // 找下一个连续的数来组成subarray, 叠加找到和为零return
            for (int j = i + 1; j < nums.length; j++){
                sum += nums[j];
                if (sum == 0){
                    result.add(i);
                    result.add(j);
                    return result;
                }
            }
        }
        return result;
    }
}

DP解法

记录index 0到array中每一个end index之和,记录到hashmap中,因为subarray一定连续的性质,如果在找到一样的和,相当于之前的end index到当前end index之间的这一段subarray的和是0;

public class Solution {
    /**
     * @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
     */
    public List<Integer> subarraySum(int[] nums) {
        List<Integer> result = new ArrayList<>();
        if (nums == null || nums.length == 0){
            return result;
        }
        
        // record accumulated sum from 0 to i
        int sum = 0;
        
        // Key: sum from 0 to end index, value of end index
        Map<Integer, Integer> map = new HashMap<>();
        
        map.put(0, -1); // 0到i idnex之和为0的情况,-1 + 1 = 0
        
        
        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
            
            
            if (!map.containsKey(sum)){
                map.put(sum, i);
            } 
            
            // 之前还有这个sum也就是说从现在的sum减去之前的sum等于0
            // 也就是说之前的end到现在end加起来等于0
            // eg. 1 2 -1 -1 
            //     1 -> 1  and  1, 2, -1, -1 -> 1  => 2, -1, -1 -> 0
            else {
                result.add(map.get(sum) + 1);  
                result.add(i);
                return result;
            }
        }
        
        return result;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • 祭祖,乃大事也,国家也对此给予国人较多方便和支持,亦是告诫:宗亲长辈,多尽孝义,千古之老,不忘寻根。 晨沐...
    米从兵阅读 190评论 0 0
  • 30年前,他跟父母一起看奥斯卡颁奖典礼,父亲问他,你拿了全世界这么多电影奖项,什么时候也拿一个小金人啊?他笑着答道...
    联动书匠阅读 277评论 0 0
  • 不知道发现,很多非常厉害的人他们都是非常喜欢折腾的,不甘于现状,总是跳出自己原有的角色,去体验新的区域,慢慢接...
    BetterThanEver阅读 149评论 0 0
  • 丈夫有一同学,老早就闻名,知她和我一样是个爱好文学的“文青”,说她是“才女”也不为过。她不仅擅长散文,对诗歌也情有...
    石头精阅读 233评论 0 1