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题是存储的当前累加和的 ...
随机推荐
- typeof(self) 的作用
block对于其变量都会形成strong reference,对于self也会形成strong reference ,而如果self本身对block也是 strong reference 的话,就会形 ...
- kibana 搜索提示挡住输入框
临时解决办法 $(".typeahead-items").css("margin-top","33px")
- NPOI 导出Excel2007版本时出现流已关闭问题
NPOI生产.xlsx文件件时,在使用book.Write(ms);后,会关闭流,这样导致再次使用Respons输出流的时候就出错了. 我看到一些网友提供的解决办法是: public class NP ...
- 如何解决前端传来的时间格式与mysql表中时间格式不匹配的查询问题
前端传过来的时间格式为“2016-07-11 11:13:10”,而数据表中对应字段`add_time`的格式为“2016-7-11”,此时sql不能直接用 "where `add_time ...
- Spark机器学习读书笔记-CH05
5.2.从数据中提取合适的特征 [root@demo1 ch05]# sed 1d train.tsv > train_noheader.tsv[root@demo1 ch05]# lltota ...
- Java特性-HashMap
想分享一个对HashMap的理解: 我们首先要知道一个HashMap对象的构成,一般的理解是:一个Map里面放了很多个键值对,合在一起就是一个键值对的数组: 大概这么理解没问题,可是有一点要说明一下, ...
- ucenter 新增用户后 自动登录discuz 不用激活
uc_client models user.php function add_user($username, $password, $email, $uid = 0, $questionid = '' ...
- DevExpress gridControl 设置分组
有些代码非常有用,但是用的时候就记不清怎么写,所以就在这里打个草稿. //设置组汇总 private void SetSummation() { this.gridViewShipment.Group ...
- eap-ttls/mschapv2
eap-ttls/mschapv2 文件路径 用途 示例 备注 #gedit /usr/local/etc/raddb/sites-available/default #gedit /us ...
- BAT技巧
FOR使用 枚举input.txt里的每一行,执行call :dosth %%i(以换行符为分割,默认是空格) for /f "delims=" %%i in (input.txt ...