题目:

给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置

样例

给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].

解题:

更新

子树的和是0,根据给的例子:[-3, 1, 2, -3, 4],其累加和【-3,-2,0,-3,1】中心出现了一个数0 ,同时发现两个-3,第一个-3开始到第二个-3之前的部分就是这个子数组

数组【1,2,3,-5,3】,其累加和【1,3,6,1,5】,两个1之间的部分就是答案,根据元素数组:不包括第一个1的位置,包括第二个1的位置

所以:计算累加和,当和的值之前已经出现了,这个区间内的数就是子数组的起始id

注意:当包括0位置的时候,包括0位置,不包括最后一个位置

当不包括0位置的时候,包括上一个数位置,不包括当前位置

这个时候需要加入一个值防止是第一个数,put(0,-1),表示还没有数的时候数是0,起始位置是-1,这样当包括0位置的情况,转化为不包括0的情况

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>();
if(nums == null || nums.length == 0){
return result;
}
HashMap<Integer,int[]> map = new HashMap<Integer,int[]>();
map.put(0,new int[]{-1});// 第一个元素为 0 id 应该为-1
int sum = 0;
for(int i=0;i<nums.length;i++){
sum += nums[i];
int[] value = map.get(sum);
if( value == null){
value = new int[]{i};
map.put(sum,value);
}else{
result.add(value[0] + 1);
result.add(i);
return result;
}
}
return result;
}
}

纯暴力,时间超时

Java程序:

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>();
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
result.add(i);
result.add(i);
return result;
}
for(int j=i+1;j<nums.length;j++){
int sum = 0;
for(int k = i;k<=j;k++){
sum+=nums[k];
}
if(sum==0){
result.add(i);
result.add(j);
return result;
}
}
}
return result;
}
}

时间复杂度:O(n3)

参考编程之美,时间复杂度降为:O(n2),这个转换其实很简单,但是有可能你就不知道

Java程序:

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>();
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
result.add(i);
result.add(i);
return result;
}
int sum = 0;
for(int j=i;j<nums.length;j++){
sum+=nums[j];
if(sum==0){
result.add(i);
result.add(j);
return result;
}
}
}
return result;
}
}

总耗时: 4127 ms

转化成Python程序:

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
"""
def subarraySum(self, nums):
# write your code here
numslen = len(nums)
for i in range(numslen):
sum = 0
for j in range(i,numslen):
sum+=nums[j]
if sum==0:
return [i,j]

总耗时: 974 ms

时间复杂度,也可以降到O(n)

这里利用到HashMap,从nums[0] 到nums[nums.length-1]的和,都添加到HashMap中,在这个求和的过程中回出现下面情况:

sum0->i = sum0->j

这里就说明sum(i+1)->j的和是0

这样时间复杂度就是O(n)

Java程序:

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
int numslen = nums.length;
ArrayList<Integer> result = new ArrayList<Integer>();
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(0,-1);
int sum = 0;
for(int i=0;i<numslen;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;
}
}

总耗时: 4520 ms

Python程序:

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
"""
def subarraySum(self, nums):
# write your code here
numslen = len(nums)
d = {0:-1}
sum = 0
for i in range(numslen):
sum = sum + nums[i]
if sum in d:
return [d.get(sum) + 1,i]
d[sum] = i

总耗时: 1361 ms

lintcode:子数组之和为0的更多相关文章

  1. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. Minimum Size Subarray Sum 最短子数组之和

    题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...

  3. [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 ...

  4. [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, ...

  5. 求数组的子数组之和的最大值III(循环数组)

    新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试 想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存 ...

  6. 求数组的子数组之和的最大值II

    这次在求数组的子数组之和的最大值的条件下又增加了新的约束:  1.要求数组从文件读取.      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保 ...

  7. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  8. lintcode :continuous subarray sum 连续子数组之和

    题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...

  9. C#中求数组的子数组之和的最大值

    <编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...

随机推荐

  1. .Net平台Winform两个ComboBox控件绑定同一个数据源

    今天WINFROM编程遇到这么一个问题:是有关WINFORM中两个comboBox控件绑定同一个数据源的问题,在窗体的界面上有两个comboBox,我在Form1_Load中对他们做了数据绑定(具体代 ...

  2. SQLServer数据库表中将指定列分组转一行

    不说明,直接看代码: --1. 创建表,添加测试数据 CREATE TABLE #test(code varchar(50), [values] varchar(10)) INSERT #test S ...

  3. 系统中使用frameset和Iframe刷新页面session失效

    问题:Asp.net中每次刷新页面,session中保存的只就丢失 原因: 1.有些杀毒软件会去扫描web.config文件 2.程序内部有让session丢失的代码,或服务器内存不足 3.程序有框架 ...

  4. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  5. javascript 数组排序之 sort()

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  6. HDU 2669 第六周 I题

    Description The Sky is Sprite.  The Birds is Fly in the Sky.  The Wind is Wonderful.  Blew Throw the ...

  7. 开发一个iOS应用没有那么容易

    导读:这是来自新加坡的 iOS 开发者 Kent Nguyen 发表在1月底的一篇博文.这篇吐槽文在 iOS 开发圈子里流传甚广,从原文150多个评论就可见一斑,现翻译如下. 让我们开门见山吧:做一个 ...

  8. floor舍去法取整

    $int = 0.99999999999999999; echo floor($int); // returns 1 $int = 0.9999999999999999; echo floor($in ...

  9. 【转载】使用Axure制作App原型怎样设置尺寸?

    使用Axure制作App原型怎样设置尺寸? 原文地址:http://www.axure.us/2172/ 本文由原型库网站投稿,转载请注明出处. 最近有几位小伙伴儿都提出同样一个疑问:想用Axure设 ...

  10. Ubuntu12.04更新源地址列表

    1. 修改更新源 sudo gedit /etc/apt/sources.list 2. 比较全的更新源列表中一般都包含deb和对应的deb-src. deb和对应的deb-src一般都包含preci ...