Subarray Sum
来源:互联网 发布:手机淘宝批量退款 编辑:程序博客网 时间:2024/04/30 14:50
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.
Notice
There is at least one subarray that it's sum equals to zero.
Example
Given [-3, 1, 2, -3, 4]
, return [0, 2]
or [1, 3]
.
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) { int l = nums.length; ArrayList<Integer> list = new ArrayList<Integer>(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(0, -1);//for the case like [1, -1] int sum = 0; for(int i = 0; i < l; i++) { sum += nums[i]; if(map.containsKey(sum)) { list.add(map.get(sum) + 1); list.add(i); return list; } map.put(sum, i); } return list; }}/**推荐解法:The idea is based on the prefix sum: Iterate through the array and for every element array【i】, calculate sum of elements form 0 to i (this can simply be done as sum += arr【i】). If the current sum has been seen before, then there is a zero sum array, the start and end index are returned.用HashMap: O(N)时间,但是more memory, 大case会MLE**/
0 0
- Subarray Sum
- Subarray Sum
- subarray sum
- Subarray Sum
- Subarray Sum
- Max-Sum Subarray
- Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- Minimum size subarray sum
- leetcode_Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- lintcode:Continuous Subarray Sum
- Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- Android Studio gradle 编译提示‘Error:Configuration with name 'default' not found.’ 解决办法
- 对报警字典类型字符串的jQuery处理
- oracle 连接数
- 熟练使用IText
- python调java
- Subarray Sum
- 编译Mplayer
- 记ajax请求出现http 406
- OpenFEA数据分析之爱户外运动的土豪在哪里?
- 条码扫描二维码扫描——ZXing android 源码简化
- java IO流实现 实现两个文本的复制
- leaflet中Layer Groups和Layers Control用法
- js代码实现轮播效果
- java8实现spark wordcount并且按照value排序输出