lintcode:子数组之和为0
题目:
子数组之和
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
给出[-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 hereArrayList<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 应该为-1int 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 hereArrayList<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;} }
View Code
时间复杂度: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 hereArrayList<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;} }
View Code
总耗时: 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 herenumslen = len(nums)for i in range(numslen):sum = 0for j in range(i,numslen):sum+=nums[j]if sum==0:return [i,j]
View Code
总耗时: 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 hereint 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;} }
View Code
总耗时: 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 herenumslen = len(nums)d = {0:-1}sum = 0for i in range(numslen):sum = sum + nums[i]if sum in d:return [d.get(sum) + 1,i]d[sum] = i
View Code
总耗时: 1361 ms
lintcode:子数组之和为0相关推荐
- 编程之美 2.14求数组的子数组之和的最大值
对于一个有N个元素的数组,a[0]~a[n-1],求子数组最大值. 如:数组A[] = [−2, 1, −3, 4, −1, 2, 1, −5, 4],则连续的子序列[4,−1,2,1]有最大的和6. ...
- 算法题 子数组之和为零
lintcode 138 子数组之和为零 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 这里给定是思路是这样的,依次求数组nums的前缀和,其前缀和的数组 ...
- lintcode 子数组问题(最全的面试子数组问题)
前言 2017年的六月份到九月份,陆陆续续在leetcode上面刷了130道题目. 眼瞅着明年开年就要开始求职找实习.于是重新开始了一波刷题. 现在的选择是使用lintcode,相比leetcode, ...
- 编程之美2.14 求数组的子数组之和的最大值
这是一个在面试中出现概率很高的一道题目,就拿我来说吧,面试了5家公司中,两家公司问了这道题目,可见,这道题目是非常经典的. 解题思想也不是很难,我熟悉的有:两种解题办法: ...
- 《团队开发一(求一个数组的连续的子数组之和的最大值)》
<团队开发一(求一个数组的连续的子数组之和的最大值)> (1)设计思想:一般的,求一个数组的最大子数组之和即是按数组顺序依次让前几个数的和与下一个数进行比较,设一变量来装每次比较后的较大的 ...
- 编程之美-2.14-求数组的子数组之和的最大值
这个以前写过,见求数组的最长子数组之和的最大值 这里说一下后面扩展题目. 1. 简述 1) 如果数组首尾相连,即允许找到一组数字(A[i],···,A[n-1], A[0],···, A[j]),请使 ...
- 求数组的子数组之和的最大值
一个有N个整数元素的一维数组( A[0], A[1], ... , A[n-2], A[n-1]),子数组之和的最大值是什么?(要求子数组的元素是连续的) 例子:有数组( -2, 5, 3, -6, ...
- 【C】课堂结对联系-求整数数组的子数组之和的最大值(党云龙、黄为)
测试题目 求整数数组的子数组之和的最大值. 题目分析 首先是明确题目的目的:求最大值:其次是考虑子数组求和.这里将求最大值写成一个单独的函数.主函数未测试函数.这里用到了二重循环,时间复杂度为N^2. ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
最新文章
- poj(2325)线段树
- 计算机等级考试属于什么培训,计算机等级是什么
- 库对比工具mysqldiff使用
- android拍照截图组件,Android截图命令screencap与视频录制命令screenrecord(示例代码)...
- 锐驰机器人的市场_【年终盘点】2020年,锐驰的王炸新品!
- mac android屏幕演示,如何在Mac上录制Android设备的屏幕 | MOS86
- php前端路由权限,SaaS-前端权限控制
- 华为android 驱动安装失败,华为手机驱动出现安装失败的问题怎样解决?
- html5 3d gallery,使用jQuery制作3d画廊房间
- 大数据apache-spark问题总结
- 应用wps对证件照进行更改颜色,更换只需三步。
- 用java根据年份判断生肖_怎样根据年份确定生肖
- 利用python通过字幕文件.srt来实现对视频片段的截取
- C++如何打开一个exe文件
- 电脑更换硬盘 | 怎么迁移数据到新硬盘?
- 关于幼儿教师音乐素养对幼儿成长影响力的研究的论文怎么写呀
- NB-IoT 智能门磁代码快速实现
- vue项目的目录结构图及目录详解
- 【天梯赛练习题(c语言)】
- SR-ITM--融合超分辨率和逆色调映射(二)
热门文章
- odoo pivot中去掉求和_评比算分,去掉最高分和最低分算平均,PLC怎样编程实现?...
- Javascript希尔排序
- aix服务器设备型号,IBM产品系列和AIX系统版本
- python花瓣长度和花瓣宽度散点图鸢尾花_Python可视化seaborn练习题
- 计算机函数left的用法,excel中的left函数怎么使用呢?
- excel 相邻数计算机,Excel计算公式大全.doc
- hadoop是什么_Hadoop精华问答 | hadoop能干什么?
- sqlserver中分组按逗号连接
- spark的朴素贝叶斯分类原理
- 【引用】成熟人格六要素