LintCode Subarray Sum

For this problem we need to learn a new trick that if your start sum up all elements in an array. When you start from a to b to sum up all elements, if the result is zero, and also the summation of all elements from index a to c ( c<=b ) is zero. Then, we need to think about sub array from index c+1 to index b is a sub array with summation of all elements to zero.
Notice: we need to formalize the summation of index =-1 to 0. Then if there is a sub array start from 0 has summation of 0, we will return the result.
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
ArrayList<Integer> result = new ArrayList<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
//set index -1 sum to be 0 then when you meet an index with sum equals
//0 you can return otherwise there is never a 0 stored for the empty
//list
map.put(0,-1);
int sum = 0;
for (int i = 0; i < nums.length; i++ ) {
sum += nums[i];
if (map.containsKey(sum)) {
result.add(map.get(sum) + 1);
result.add(i);
return result;
}
map.put(sum,i);
}
return result;
}
}
LintCode Subarray Sum的更多相关文章
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- [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 ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- LintCode "Subarray Sum II"
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
随机推荐
- repcache实现memcached主从
1.repcached介绍 repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步 ...
- restful 部署 PUT,DELETE 请求发生405 谓词Http不允许
在部署Restful服务的时候,发现中间有个PUT请求的服务,报405 谓词HTTP 不允许解决方法:卸载WebDav 如果不行可以尝试下另一种方式:
- Bootstrap<基础二十> 标签
Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .label 来显示标签,如下面的实例所示: <!DOCTYPE html> <html> ...
- html5移动web开发笔记(一)Web 存储
localStorage - 没有时间限制的数据存储 localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. 用户访问页面的次 ...
- sysctl.conf网络内核参数说明(转)
下面是我的理解,可能有误,仅供参考. 要调优,三次/四次握手必须烂熟于心. client server(SYN_SENT) —> (SYN_RECV ...
- SQL Server中cursor的使用步骤
参考文章: http://www.cnblogs.com/knowledgesea/p/3699851.html http://www.cnblogs.com/moss_tan_jun/archive ...
- centos上libreoffice+unoconv安装步骤,实现word转pdf
一.libreoffice安装 1.yum search libreoffice查询一下系统自带的安装包 安装libreoffice.x86_64这个就可以了 2.yum install lib ...
- ubuntu 16.04 搭建无线共享热点(PC 无线直连Android移动终端 调试,监控屏幕)
由于android终端usb接口比较松,公司的无线网络使用人过多比较慢,所以想使用PC端无线直连 调试Android终端 配置无线共享 1 网卡要支持 2 安装 plasma-nm sudo apt- ...
- web.config设置和取值
博客园中有一篇文章对web.config的结构做了很详细的介绍,原文见 http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.htm ...
- Isolation-based Anomaly Detection
Anomalies are data points that are few and different. As a result of these properties, we show that, ...