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题是存储的当前累加和的 ...
随机推荐
- CentOS 5系统安装Django、Apache 、mod_wsgi部署Python环境教程
Django,是一款针对Python环境的WEB开发框架,能够帮助我们构架快捷.简单的WEB框架设置,Django框架非常适合开发内容应用环境,所以在本文中,麦子将整理基于Centos系统部署安装Dj ...
- Android Fragment
1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...
- AngularJS身份验证:Cookies VS Tokens
基于cookie的身份验证:Cookie-Based Authentication 基于token的身份验证:Token-Based Authentication 跨域:cross-domain 说明 ...
- 0x7c95caa2指令引用的0x00000000内存 该内存不能read
出现这样的错误,往往和动态库有关系! 解决方法:
- 对客户推荐产品模型+python代码
首先观看数据: l 数据的基本特征用 describe 描述每个基本特征 l 画图画出每个特征的基本统计图 应用import matplotlib.pylab as pl 画图显示 l 关 ...
- KTV项目 SQL数据库的应用 结合C#应用窗体
五道口北大青鸟校区 KTV项目 指导老师:袁玉明 歌曲播放原理 SQL数据库关系图 C#解决方案类图 第一步:创建数据库连接方法和打开方法和关闭方法! public class DBHelper { ...
- 火狐浏览器不能获取 event,解决js报错问题
//←的keyCode等于:37 //→的keyCode等于:39 //↑的keyCode等于:38 //↓的keyCode等于:40 function keystoke(obj){ //var e ...
- IOS BLE蓝牙4.0
前言: 自己做的项目里面有这么一个功能,总结归纳一下. 先导入必要的框架 CoreBluetooth.framework 在要用到蓝牙的文件里面导入以下头文件 #import <CoreBlu ...
- CTE
Why use CTE: Improve code readability Breaking up complex queries into smaller blocks Execution Scop ...
- vs2010无可用源
全选CPP文件内容,选择 “编辑”-“高级”-“设置选定内容的格式”,保存,重新编译. 当然这种方法是不能完全解决这个问题的奥,你需要在菜单栏的生成里面找到重新生成解决方案,重新生成解决方案试一下啦, ...