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题是存储的当前累加和的 ...
随机推荐
- 【编辑器】【Sublime Text】使用笔记
1.安装 官网下载即可 2.插件 sublime-text - Sublime Text 怎么高亮 Markdown 的文件语法 设置Sublime为VIM模式 如何在sublime 里面设置 ver ...
- JS基础(超级简单)
1 JS基础(超级简单) 1.1 数据类型 1.1.1 基本类型: 1) Number:特别注意:NaN的检测方法:Nan!=NaN;或者使用isNaN方法 2) ...
- div随意拖动小例子
<html> <head> <title> Drag Demo 1 </title> <style type="text/css&quo ...
- 点击document隐藏某个div
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript 闭包系列一
一. 闭包的概念 闭包是有权访问另一个函数作用域中的变量的函数. 如下代码:根据变量作用域,函数outer中所有的局部变量对函数inner都是可见的.但是反过来不行,inner内部的局部变量对oute ...
- C#自定义属性(跟成员变量的区别)
属性声明 public int age { get; set; } 从功能上等价于 private int m_age; public int age {get { return m_age; }se ...
- C++ 为什么拷贝构造函数参数必须为引用?赋值构造函数参数也必须为引用吗?
之前写拷贝构造函数的时候,以为参数为引用,不为值传递,仅仅是为了减少一次内存拷贝.然而今天看到一篇文章发现自己对拷贝构造的参数理解有误. 参数为引用,不为值传递是为了防止拷贝构造函数的无限递归,最终导 ...
- 曲线拟合的最小二乘法(基于OpenCV实现)
1.原理 在现实中经常遇到这样的问题,一个函数并不是以某个数学表达式的形式给出,而是以一些自变量与因变量的对应表给出,老师讲课的时候举的个例子是犯罪人的身高和留下的脚印长,可以测出一些人的数据然后得到 ...
- VMware-workstation-full-11.0.0-2305329&VMware-player-7.0.0-2305329
VMware-workstation-full-11.0.0-2305329.exe Name: VMware-workstation-full-11.0.0-2305329.exe 发行日期: 20 ...
- 安全协议系列(三)----CCMP与WPA-PSK
本节讨论 CCM 在 WiFi 中的实际应用 -- CCMP 协议 根据 RFC 3610,完成 CCMP 报文的加解密,需要提供:分组密钥(K).随机数(Nonce).附加认证数据(AAD),这三个 ...