Subarray Sum

原题链接:http://lintcode.com/zh-cn/problem/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.

样例

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

标签 Expand

SOLUTION 1:

我们有一个O(N)的解法。使用Map 来记录index, sum的值。当遇到两个index的sum相同时,表示从index1+1到index2是一个解。

注意:添加一个index = -1作为虚拟节点。这样我们才可以记录index1 = 0的解。

空间复杂度:O(N)

  1. public class Solution {
  2. /**
  3. * @param nums: A list of integers
  4. * @return: A list of integers includes the index of the first number
  5. * and the index of the last number
  6. */
  7. public ArrayList<Integer> subarraySum(int[] nums) {
  8. // write your code here
  9.  
  10. int len = nums.length;
  11.  
  12. ArrayList<Integer> ret = new ArrayList<Integer>();
  13.  
  14. HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  15.  
  16. // We set the index -1 sum to be 0 to let us more convient to count.
  17. map.put(0, -1);
  18.  
  19. int sum = 0;
  20. for (int i = 0; i < len; i++) {
  21. sum += nums[i];
  22.  
  23. if (map.containsKey(sum)) {
  24. // For example:
  25. // -3 1 2 -3 4
  26. // SUM: 0 -3 -2 0 -3 1
  27. // then we got the solution is : 0 - 2
  28. ret.add(map.get(sum) + 1);
  29. ret.add(i);
  30. return ret;
  31. }
  32.  
  33. // Store the key:value of sum:index.
  34. map.put(sum, i);
  35. }
  36.  
  37. return ret;
  38. }
  39. }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/array/SubarraySum.java

Lintcode: Subarray Sum 解题报告的更多相关文章

  1. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  2. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  7. 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...

  8. [LintCode] Subarray Sum & Subarray Sum II

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  9. USACO Section2.3 Zero Sum 解题报告 【icedream61】

    zerosum解题报告----------------------------------------------------------------------------------------- ...

随机推荐

  1. 完善_IO, _IOR, _IOW, _IOWR 宏的用法与解析

    _IO, _IOR, _IOW, _IOWR 宏的用法与解析  原文地址:http://www.eefocus.com/ayayayaya/blog/12-03/245777_20cdd.html 作 ...

  2. AndroidStudio升级到2.3版本无法编译的解决方法

    上周五as提示更新,于是为了体验新功能还在编码过程中就迫不及待的点击了更新,公司网很快,十几分钟就下载好,然后一重启就懵逼了,提示是否更改依赖版本到2.3以及升级gradle到3.3,点了确定就一直在 ...

  3. iOS-获取当前View所在的控制器

    用一个分类,具体: .h #import <UIKit/UIKit.h> @interface UIView (CurrentController) /** 获取当前View的控制器对象 ...

  4. java.lang.IllegalStateException: No typehandler found for mapping XXX

    前言:今天遇到了这个问题,刚开始觉得很容易解决的,毕竟能定位到出问题的文件和对应的字段,根据以往的经验也可以判断出是字段映射类型不匹配的问题,后来找了半天还是没找到问题的根源,从网上百度,也没看到令人 ...

  5. git web开发版本管理

    使用git来管理web开发: 我们需要做的事情 : 1,在服务器建立版本仓库: 2,在服务器建立稳定版本的站点,编写版本仓库的hooks: 3,在开发服务器上提交开发版本: 下面一步一步来:(注意建立 ...

  6. [aaronyang] nodejs学习-mongodb[1]

    1.资源提供与安装(ayjs.net) 学习说明:nodejs还是在非windows环境下操作好,所以一切还是 当前时间:2014年12月06日aaronyang 官网地址:www.mongodb.o ...

  7. Linux下实现脚本监测特定进程占用内存情况

    Linux系统下,我们可以利用以下命令来获取特定进程的运行情况: cat /proc/$PID/status 其中PID是具体的进程号,这个命令打印出/proc/特定进程/status文件的内容,信息 ...

  8. appium简明教程(4)——appium client的安装

    appium client是对webdriver原生api的一些扩展和封装.它可以帮助我们更容易的写出用例,写出更好懂的用例. appium client是配合原生的webdriver来使用的,因此二 ...

  9. Jmeter 线程组、运行次数参数化

    Jmeter的jmx文件保存了线程数和运行次数等参数,这个参数可以在命令行中传入参数的方式来修改数值 步骤如下 1.生成线程和运行次数的参数 Jmeter选项中函数助手对话框,选中__P参数,这个参数 ...

  10. 修改eclipse.ini文件指定jdk

    在eclipse.ini文件顶部插入 -vmE:/kfgj/Java/jdk1.7.0_21/bin/javaw.exe