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. Linux 下Shell的学习

    1.Shell学习 1.什么是Shell    shell是一个命令解析器,在操作系统的最外层,负责和用户对话,将用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果.2.什么是shell脚本 ...

  2. 掷骰子游戏窗体实现--Java初级小项目

    掷骰子 **多线程&&观察者模式 题目要求:<掷骰子>窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰 ...

  3. winform中webBrowser模拟网页操作中遇到的问题

    我们通过网页上传一些特殊数据的时候,由于必填项众多,数量量大的时候,会发现工作相当繁琐,前段时间做了一个winform内嵌webBrowser模拟网页上传文档的小工具,发现了许多问题,总结一下: 先说 ...

  4. CentOS 6 各种启动文件损坏及修复

    stage1 mbr的破坏和恢复 清空mbr 前446字节 dd if=/dev/zero of=/dev/sda bs=1 count=446 如果没有挂载启动光盘,会显示这样 如果启动前挂载了光盘 ...

  5. UpdatePanel中用后台CS代码调用JS代码,先执行控件事件,后触发JS

    引用地址: http://www.cnblogs.com/silenkee/articles/1609831.html   页面中加入了UpdatePanel后,Response.Write(&quo ...

  6. 017.1 stringBuffer

    内容:String Buffer/String Builder方法 + 两个简单练习是缓冲区,最后都会转成字符串处理,有局限性###########方法添加元素:     .append()插入元素: ...

  7. lisp base

    一 .quote lisp 使用s-expr表示数据和代码,通常会将第一项作为函数,而将后续元素当做参数传给第一项进行计算.可以通过quote来进行其他解析,quote可用(‘)表示: ( + 1 1 ...

  8. 远程调用内核接口的封装类(RCKObjs)

    RCK 包括 Application, Function, Connection, Command, Response 和 Fields 六 大类, 其主要功能例如以下:     a. Applica ...

  9. 架构图以及vue的简介

    架构图 前后端分离总架构图 前端架构设计图 MVVM架构模式 MVVM的简介 MVVM 由 Model,View,ViewModel 三部分构成,Model 层代表数据模型,也可以在Model中定义数 ...

  10. elk6.*版本搭建连接 比较好一点的

    https://www.cnblogs.com/harvey2017/p/8922164.html