Description

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.

There is at least one subarray that it's sum equals to zero.

Example

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

分析:题目给定一个数组,让你截取和为0的子数组,并返回该子数组的起点和终点的下标。先说一下我的思路,虽然时间复杂度达到平方级,但总的来说比较容易理解。外循环i表示子数组的起始下标,内循环表示以i为起点的子数组的终点下标。循环过程中,只要发现sum=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 List<Integer> subarraySum(int[] nums) {
// write your code here
List<Integer>list=new ArrayList<Integer>();
if(nums.length==1&&nums[0]==0){
list.add(0);
list.add(0);
}
for(int i=0;i<nums.length-1;i++){
int sum=0;
list.clear();
list.add(i);
for(int j=i;j<nums.length;j++){
sum+=nums[j];
if(sum==0){
list.add(j);
return list;
}
}
}
return list;
}
}

在网上看到另一种思路,线性级,感觉挺好的。利用哈希表,存储从起点到每个位置的和,如果哪两个位置的和相等,说明这两个数之间的子数组里的所有数的和为0。有点绕口,举个例子

有如下数组:

3,1,2,-1,2,2,1,-3,5   每个位置对应的和为:

3    4   6     5    7   9   10   7    13   发现有两个七,那么中间的那个子数组相当于没加,即【2,1,-3】和为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 int len = nums.length; ArrayList<Integer> ans = new ArrayList<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(0, -1); int sum = 0;
for (int i = 0; i < len; i++) {
sum += nums[i]; if (map.containsKey(sum)) {
ans.add(map.get(sum) + 1);
ans.add(i);
return ans;
} map.put(sum, i);
} return ans;
}
}

138. Subarray Sum【Lintcode,by java】的更多相关文章

  1. 蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法 【原创,多图】

    蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法 [原创,多图] spm=a1z10.1.w4004-5319414070.11.Zd ...

  2. Solr基础理论【倒排索引,模糊查询】

    一.简介 现有的许多不同类型 的技术系统,如关系型数据库.键值存储.操作磁盘文件的map-reduce[映射-规约]引擎.图数据库等,都是为了帮助用户解决颇具挑战性的数据存储与检索问题而设计的.而搜索 ...

  3. TCP协议三次握手过程分析【图解,简单清晰】

    转自:http://www.cnblogs.com/rootq/articles/1377355.html TCP(Transmission Control Protocol) 传输控制协议 TCP是 ...

  4. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  5. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  6. 【Aspose.Words for Java】 对word文档,增加页眉,页脚,插入内容区图像,

    一.环境准备 jar包:aspose-words-20.4.jar 或者去官方网站下载: 官方网站:https://www.aspose.com/ 下载地址:https://downloads.asp ...

  7. 209. Minimum Size Subarray Sum【滑动窗口】

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

  8. 【Java EE 学习 71 上】【数据采集系统第三天】【增加页面】【增加问题】【编辑页面,编辑问题】

    增加页面和编辑页面.增加问题和编辑问题的页面使用的都是相同的页面,最后调用的方法是saveOrUpdate方法,所以只说一个就可以了. 一.增加页面 比较简单,略.流程如下: 单击“增加页”超链接-& ...

  9. 1. 两数之和【Leetcode中国,by java】

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...

随机推荐

  1. Java学习---JBPM[工作流]学习

    点击参考 更多参考 链接:https://pan.baidu.com/s/1A1AH5sXu7yhV3mncpH4Z6w 密码:88zw

  2. OSG3.0.1的编译

    在OSG-中国有很多关于OSG的资料,包括OSG的编译和教程. 要编译OSG首先需要准备的包: 1,OSG3.0.1源代码: 2,CMAKE: 3,OSG用到的第三方库: 4,OSG Data:OSG ...

  3. 4星|《助推(实践版)》:英国政府用AB测试检验政策效果的经验

    助推:小行动如何推动大变革(实践版)(诺贝尔经济学奖得主理查德•塞勒的助推实践) 作者作为学者说服英国政府实施助推策略的经过,提到一些具体主推策略. 所谓的助推,很像IT业流行的AB测试,对政策的执行 ...

  4. 只用最适合的! 全面对比主流 .NET 报表控件:水晶报表、FastReport、ActiveReports 和 Stimulsoft

    前言 随着 .NET 平台的出现,报表相关的开发控件随之出现,目前已经有若干成熟的产品可供开发人员使用,本文旨在通过从不同维度对比目前最流行的4款 .NET报表控件,给所有报表开发人员在做产品选型时一 ...

  5. Java.util 包(Date 类、Calendar类、Random类)

    java.util 包提供了一些实用的方法和数据结构. Date 类 Date 类表示日期和时间,里面封装了操作日期和时间的方法.Date 类经常用来获取系统当前时间. 构造方法: 构造方法 说明 D ...

  6. angularJs的$scope.$apply

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  7. django admin后台插件:django-suit入门

    去年9月底开始用django来做公司内部项目,开始对django有了一些了解,感觉django真的蛮强大的(也有很多人推荐flask,将来有空的话我会试试).今天的话只是介绍一个小东西,django管 ...

  8. CSS(层叠样式表)基础知识

     CSS 指层叠样式表 (Cascading Style Sheets).样式定义怎样显示 HTML 元素.它通常存储在样式表中,把样式加入到 HTML 4.0 中,解决内容与表现分离的问题. 当同一 ...

  9. 【[TJOI2017]异或和】

    这道题挺神仙的,毕竟这个异或是需要进位的 看到区间和我们很自然的就想到了前缀和 于是处理一下前缀和答案就变成了这个样子 \[⊕\sum_{i=1}^n\sum_{j=1}^{i}pre_i-pre_{ ...

  10. 8、Spring Cloud-配置中心 Spring Cloud Config(待补充)

    8.1.Config Server 本地读取配置文件 Config Server 可以从本地仓库读取配置文件,也可以从远处 Git 仓库读取.   本地仓库是指将所有的配置文件统 写在 Config ...