138. Subarray Sum【Lintcode,by java】
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】的更多相关文章
- 蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法 【原创,多图】
蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法 [原创,多图] spm=a1z10.1.w4004-5319414070.11.Zd ...
- Solr基础理论【倒排索引,模糊查询】
一.简介 现有的许多不同类型 的技术系统,如关系型数据库.键值存储.操作磁盘文件的map-reduce[映射-规约]引擎.图数据库等,都是为了帮助用户解决颇具挑战性的数据存储与检索问题而设计的.而搜索 ...
- TCP协议三次握手过程分析【图解,简单清晰】
转自:http://www.cnblogs.com/rootq/articles/1377355.html TCP(Transmission Control Protocol) 传输控制协议 TCP是 ...
- 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 ...
- 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 ...
- 【Aspose.Words for Java】 对word文档,增加页眉,页脚,插入内容区图像,
一.环境准备 jar包:aspose-words-20.4.jar 或者去官方网站下载: 官方网站:https://www.aspose.com/ 下载地址:https://downloads.asp ...
- 209. Minimum Size Subarray Sum【滑动窗口】
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 【Java EE 学习 71 上】【数据采集系统第三天】【增加页面】【增加问题】【编辑页面,编辑问题】
增加页面和编辑页面.增加问题和编辑问题的页面使用的都是相同的页面,最后调用的方法是saveOrUpdate方法,所以只说一个就可以了. 一.增加页面 比较简单,略.流程如下: 单击“增加页”超链接-& ...
- 1. 两数之和【Leetcode中国,by java】
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
随机推荐
- 深入浅出SharePoint——获取Choice Field的Mapping value
list field对应的caml定义如下 <Field Type="Choice" DisplayName="Inspection Result" Re ...
- 【2017.10.13 ROS机器人操作系统】ROS系统常用术语及资源
ROS机器人操作系统是一种后操作系统,提供了类似于软件开发中使用到的中间件的功能. ROS: Robot Operating System 机器人操作系统 Package: 功能包 Stack: 功能 ...
- js布局库
1.viz.js The solution was that someone cross compiled Graphviz to Javascript using llvm + emscripten ...
- Running Protractor Tests on Docker
配置这个Protractor环境真是折磨死人了,webdriver-manage update怎么都不成功,即使自己下载好chromederiver放到相应文件夹下,也不能使用.费时三四天终于按照ht ...
- mysql-存储过程(转载)
本来想自己写存储过程的,但是看到别人写的很全面,就直接转载过来了. 转自(http://www.cnblogs.com/exmyth/p/3303470.html) 14.1.1 创建存储过程 MyS ...
- Input and Output-The input is all the sources of action for your app
Programs take input and produce output. The output is the result of doing something with the input. ...
- AESUtil 加密
package com.hxqc.basic.dependency.util; import org.apache.commons.lang.StringUtils; import sun.misc. ...
- BZOJ1048:[HAOI2007]分割矩阵(记忆化搜索DP)
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个), 这样分割了(n-1)次后,原矩阵被分割成 ...
- Eclipse导入web项目发布项目时报Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web错误解决方案
Eclipse导入web项目后,将web项目加载到server进行发布时,提示Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java ...
- Kali-linux免杀Payload生成工具Veil
Veil是一款利用Metasploit框架生成相兼容的Payload工具,并且在大多数网络环境中能绕过常见的杀毒软件.本节将介绍Veil工具的安装及使用. 在Kali Linux中,默认没有安装Vei ...