LintCode-子数组之和
题目描述:
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
给出 [-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 ArrayList<Integer> subarraySum(int[] nums) {
ArrayList<Integer> pos = new ArrayList<Integer>();
if(nums.length==1 && nums[0]==0){
pos.add(0);
pos.add(0);
return pos;
}
int[] preSum = new int[nums.length];
int t=0;
for(int i=1;i<nums.length+1;i++){
Integer sum = 0;
for(int j=0;j<i;j++){
sum += nums[j];
}
if(sum==0){
pos.add(0);
pos.add(i-1);
return pos;
}
for(int k=0;k<preSum.length;k++){
if(preSum[k]==sum){
pos.add(k+1);
pos.add(i-1);
return pos;
}
}
preSum[t++] = sum;
}
return pos;
}
}
LintCode-子数组之和的更多相关文章
- lintcode:子数组之和为0
题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- 求数组的子数组之和的最大值III(循环数组)
新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试 想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存 ...
- 求数组的子数组之和的最大值II
这次在求数组的子数组之和的最大值的条件下又增加了新的约束: 1.要求数组从文件读取. 2.如果输入的数组很大, 并且有很多大的数字, 就会产生比较大的结果 (考虑一下数的溢出), 请保 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- C#中求数组的子数组之和的最大值
<编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...
随机推荐
- Ueditor和CKeditor 两款编辑器的使用与配置
一丶ueditor 百度编辑器 1.官方文档,演示,下载地址:http://ueditor.baidu.com/website/index.html 2.百度编辑器的好:Editor是由百度web前端 ...
- SQL Server 2012学习笔记 2 Server Core中命令行安装SQL
Setup.exe /qs /ACTION=Install /FEATURES=SQLEngine,Replication /INSTANCENAME=MSSQLSERVER /SQLSVCACCOU ...
- asp.net、html、javascript等比较有用的网站
Learn模块下web.mvc.razor等. http://www.asp.net/ 包括html.javascript.jquery.xml等教程. http://www.w3school.com ...
- 消息处理之performSelector
performSelector和直接调用方法的区别 performSelector: withObject:是在iOS中的一种方法调用方式.他可以向一个对象传递任何消息,而不需要在编译的时候声明这些方 ...
- 堆分配与栈分配---SAP C++电面(5)/FEI
一直以来总是对这个问题的认识比较朦胧,我相信很多朋友也是这样的,总是听到内存一会在栈上分配,一会又在堆上分配,那么它们之间到底是怎么的区别呢?为了说明这个问题,我们先来看一下内存内部的组织情况. 从上 ...
- aJax学习之Ajax工作原理
转自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/18/2216553.html 在写这篇文章之前,曾经写过一篇关于AJAX技术的 ...
- Hibernate学习之检索策略
一.类级别的检索策略 类级别可选的检索策略包括立即检索和延迟检索, 默认为延迟检索 –立即检索: 立即加载检索方法指定的对象 –延迟检索: 延迟加载检索方法指定的对象,在使用具体的属性时,再进行加载 ...
- Labview学习之波形图表的历史数据
Labview学习之波形图表的历史数据 默认的情况下,波形图表显示100个点, 因为波形图表默认的缓冲区大小为1024,在默认的情况下如果修改图形图标属性中的标尺项,选中自动调整标尺,如图:2011- ...
- 关于Scrapy框架的基本概念
Scrapy爬取网页基本概念 Scrapy爬取网页基本概念 怎么样用Scrapy生成project? scrapy startproject xxx 如何用Scrapy爬取网页? import scr ...
- ExpandableListView 箭头样式
ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项.ExpandableListVive ...