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)

 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 ArrayList<Integer> subarraySum(int[] nums) {
// write your code here int len = nums.length; ArrayList<Integer> ret = new ArrayList<Integer>(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); // We set the index -1 sum to be 0 to let us more convient to count.
map.put(0, -1); int sum = 0;
for (int i = 0; i < len; i++) {
sum += nums[i]; if (map.containsKey(sum)) {
// For example:
// -3 1 2 -3 4
// SUM: 0 -3 -2 0 -3 1
// then we got the solution is : 0 - 2
ret.add(map.get(sum) + 1);
ret.add(i);
return ret;
} // Store the key:value of sum:index.
map.put(sum, i);
} return ret;
}
}

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. Android百度地图相关内容汇总

    Android百度地图知识讲解 1.百度地图开发环境搭建    http://www.apkbus.com/android-116050-1-1.html 2.Android百度地图系列教程    h ...

  2. 搭建简单的网络部分(OC)框架

    准备工作 1.文件目录结构示图(按照MVC分层) 文件目录结构图/自定义Cell Controller: CYXOneViewController Model: CYXMenu View: CYXCe ...

  3. 【转】fileno函数与ftruncate函数

      fileno函数与ftruncate函数 2011-10-25 10:03:33 分类: LINUX fileno()函数 功    能:把文件流指针转换成文件描述符相关函数:open, fope ...

  4. 自动化安装smokeping-2.6.11脚本

    自动化安装Smokeping-2.6.11脚本 一.目的 1.1 监控目的 为方便监测各数据中心网络状况,自定义全国各节点,从而发现网络异常,判断网络故障. 1.2 本文目的 快速部署Smokepin ...

  5. 【Linux】参数代换命令xargs

    xargs 是在做什么的呢?就以字面上的意义来看,x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思! xargs 可以读入 ...

  6. 那些令人喷饭的代码注释:仅以此代码献给...it's realy ?

    程序源代码中的注释经常是一个卧虎藏龙的地方,有人就很喜欢写幽默搞笑的注释内容.解释代码含义的同时,也带给人轻松神经的机会,确实是很有意思的风格,来看看这一辑国外某公司产品中的注释. 注意:看的时候严禁 ...

  7. [转]Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties

    以前解析json用的惯的就是Google的gson了,用惯了基本就用它了,一直也没发现什么大问题,因为都是解析简单的json数据.但是最近学习springboot,要解析一个比较复杂的json数据.就 ...

  8. U811.1接口EAI系列之五--材料出库--VB语言

    主要业务有:09其他出库单 11:材料出库单 32:销售出库单 主要业务代码: '材料出库生成XML Public Function xml_storeout(ds_head As MSHFlexGr ...

  9. Windows8.1远程桌面时提示凭据不工作的解决方案

    本人两台电脑都是win8.1.首先确认以下三点: 1.密码没有错 2.用户连接没有达到上线(只有我一个人尝试连) 3.该用户已开启远程连接 此时还说凭据不工作的原因是域的问题,因为mstsc默认使用M ...

  10. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...