Redian新闻
>
蛋蛋家阿童木 2 in 1 今天打折 $99
avatar
蛋蛋家阿童木 2 in 1 今天打折 $99# PDA - 掌中宝
S*C
1
Subarray Sum
20%
Accepted
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.
Example
Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].
http://www.lintcode.com/en/problem/subarray-sum/
下面的解法总是在第16个test case Memory Limit Exceeded
public ArrayList subarraySum(int[] a) {
ArrayList res = new ArrayList();
//a map between sum and index
HashMap map = new HashMap();
// We set the index -1 sum to be 0 to let us more convenient to
count.
map.put(0, -1);
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
if (map.containsKey(sum)) {
// For example:
// -3 1 2 -3 4
// SUM: 0 -3 -2 0 -3 1
// then we got the solution is : 0 - 2
res.add(map.get(sum) + 1);
res.add(i);
return res;
}
// Store the key:value of sum:index.
map.put(sum, i);
}
return res;
}
avatar
n*t
2
Z3735F 10 寸 2/32G referb, acer one s1002,兄弟当年 299 还是 399 买的,还花
了 100 大毛买键盘,如今不知道躺哪吃灰了
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。