Lintcode: Subarray Sum 解题报告
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 解题报告的更多相关文章
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...
- [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 ...
- USACO Section2.3 Zero Sum 解题报告 【icedream61】
zerosum解题报告----------------------------------------------------------------------------------------- ...
随机推荐
- 【RS】Local Latent Space Models for Top- N Recommendation-利用局部隐含空间模型进行Top-N推荐
[论文标题]Local Latent Space Models for Top- N Recommendation (KDD-2018 ) [论文作者]—Evangelia Christakopou ...
- MATLAB 不能保存变量问题及解决办法
在使用matlab保存结构体.元胞数组等等的变量时,matlab总是提示 警告: 未保存变量 'session'.对于大于 2GB 的变量,请使用 MAT 文件版本 7.3 或更高版本. 问题如下: ...
- mysql--SQL编程(基础知识) 学习笔记1
1.数据库应用类型分类: 一般来说,可将数据库的应用类型分为OLTP(OnLine TransactionProcessing ,联机事务处理)和OLAP(OnLine Analysis Proces ...
- DPDK的安装与绑定网卡(转)
from:http://www.cnblogs.com/mylinuxer/p/4274178.html DPDK的安装与绑定网卡 DPDK的安装有两种方法: 第一种是使用dpdk/tools/set ...
- kafka负载均衡相关资料收集(一)
key为null时Kafka会将消息发送给哪个分区? 当你编写kafka Producer时, 会生成KeyedMessage对象. 1 KeyedMessage<K, V> keyedM ...
- VS2010启动多个实例调试
项目中经常出现一个解决方案里面有多个程序,如果想按F5启动多个实例进行操作调试那该怎么操作呢? 以前自己都使用附加进程的方法调试,这样的调试不需要按F5,自己只要运行多个程序后,使用vs的附加进程到对 ...
- Eclipse自动部署项目到Tomcat的webapps下的有效方法
开发JavaEE项目,常用的工具有MyEclipse,Eclipse,netBeans等,我比较喜欢用Eclipse,因为相比MyEclipse体积小很多,响应速度也快,且足以满足需求,我喜欢简洁的编 ...
- django 文档生成器
参考博客:http://www.liujiangblog.com/course/django/160
- Java操作XML的JAXB工具
在java中操作XML的工作中中,比较方便的工具是JAXB(Java Architecture for XML Binding). 利用这个工具很方便生成XML的tag和Java类的对应关系.参照网上 ...
- ace admin后台模板
http://ace.jeka.by/index.html 下载: http://www.daimajiayuan.com/plus/download.php?open=0&aid=18152 ...