(138)子数组之和
2015-10-22 14:01
288 查看
容易 子数组之和
查看运行结果
25%通过
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
您在真实的面试中是否遇到过这个题?
Yes
样例
给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [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 static ArrayList<Integer> subarraySum(int[] nums) {
ArrayList<Integer> list = new ArrayList<Integer>();
int len =nums.length;
int i = 0;
boolean flag = true;
while(i<len && flag)
{
int sum = nums[i];
if(sum==0)
{
list.add(i);
list.add(i);
flag = false;
}
else{
for(int j=i+1;j<len;j++)
{
sum += nums[j];
if(sum == 0)
{
list.add(i);
list.add(j);
flag = false;
break;
}
}
}
i++;
}
return list;
}
}
相关文章推荐
- ArcGIS教程:收缩 (Spatial Analyst)
- 关于数组TOP K算法(快排及最小堆方式C代码)
- MYSQL语法速查之表更改
- CentOS7 bonding配置
- Gradle Android Studio basic
- 数据库分享一: MySQL的Innodb缓存相关优化
- ubuntu下android源码下载
- Linux系统文件系统详解
- Trident State 详解
- C++多态的实现原理
- js 保存用户7天
- 重新认识Java线程的概念
- C++浅拷贝、深拷贝及引用计数浅析
- css3 transition 动画
- iOS 文件下载
- JavaScrip调试技巧之断点调试
- [HTML5学习]HTML5课程大纲介绍_工具准备课程
- 使用扩展方法(Chapter3 P39-41)
- ICE学习(七)-线程池
- 七牛云存储之图片的上传【PHP】