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. git操作:

    error: Your local changes to the following files would be overwritten by merge: **/**/**.php Please, ...

  2. python29 excel写模块xlwt

    xlwt模块用于新建excel文件并写入数据. 安装 pip install xlwt 简单使用 import xlwt from datetime import datetime #样式 style ...

  3. LoadRunner 测试Socket接口函数说明

    lrs_save_param_ex是lrs_save_param的扩展函数,包含了lrs_save_param的基本功能.其函数语法结构如下: int lrs_save_param_ex ( char ...

  4. xss实现获取内网ip

    前提得浏览器支持webRTC,测试的时候google浏览器测试成功,火狐浏览器不支持webRTC, 再在xss平台直接复制如下js代码: function form_ip(ip,port){ var ...

  5. [Python web开发] Web框架开发基础 (一)

    Python WEB框架 WSGI,WEB Server Gateway Interface,可以看做是一种底层协议,它规定了服务器程序和应用程序各自实现上面接口.Python的实现称为wsgiref ...

  6. eclipse主题皮肤设置

    这里先声明,下面的方式适合最新版本的Eclipse Luna,旧的版本可以下载我提供的这个插件,并将其放在eclipse目录下的plugins目录下即可. 插件下载地址:http://download ...

  7. map详讲<二>

    查找元素: Map可以根据健来查找元素,提供方法find(key),如果是这个健对应的元素存在,则返回的是这个健的迭代器iterator,否则返回的是std::end(): 使用find()函数有点笨 ...

  8. BigDecimal.setScale用法总结

    1. BigDecimal num1 = new BigDecimal(2.225667);//这种写法不允许,会造成精度损失 2. BigDecimal num2 = new BigDecimal( ...

  9. UIWebView 获取网页标题

    - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *urlString = webView.request.URL.absolu ...

  10. HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...