Subarray Sum
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.
Given [-3, 1, 2, -3, 4]
, return [0, 2]
or [1, 3]
.
考察集合
思路: 累加数组的每一个元素,当和出现重复时表明该范围内存在子数组子和为0的情况。
1.建立HashMap key:为数组的值,value:为数组的下标。
2.遍历数组求和。查找和是否在HashMap中出现,出现则表明存在子数组和为0。否则,将sum和下标加入map中。
public class Solution {public ArrayList<Integer> subArraySum (int[] nums) {ArrayList<Integer> ans = new ArrayList<>();if (nums == null || nums.length == 0) {return 0;}HashMap<Integer, Integer> map = new HashMap<>();map.put(0, -1);int sum = 0;for (int i = 0; i < nums.length; i++) {sum += nums[i];if (map.containsKey(sum)) {ans.add(map.get(sum) + 1);ans.add(i);} else {map.put(sum, i);}}return ans;} }
转载于:https://www.cnblogs.com/FLAGyuri/p/5260516.html
Subarray Sum相关推荐
- LeetCode Subarray Sum Equals K
原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [JAVA]寻找满足和的最短子序列(Minimum Size Subarray Sum)
题目来源:leetcode 题目描述: Given an array of n positive integers and a positive integer s, find the minimal ...
- Lintcode Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- LeetCode 523. Continuous Subarray Sum
题目: Given a list of non-negative numbers and a target integer k, write a function to check if the ar ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- LeetCode-Easy部分中标签为Dynamic Programming 523. Continuous Subarray Sum
原题 Given a list of non-negative numbers and a target integer k, write a function to check if the arr ...
- Leetcode209-Minimum Size Subarray Sum
新的个人博客:Hu Haoyu's Blog,欢迎参观! Given an array of n positive integers and a positive integer s, find th ...
最新文章
- Bzoj3730 震波
- 2018“硅谷技划”随笔(一):再论中美员工福利巨大差距的背后
- 将Python源码编译成pyc和pyo文件
- 牛客21297 手机号码
- 最新LAMP源码搭建网站平台PHP5.5.1 + Apache2.4.6 + mysql5.6.12
- Android中背景透明的Dialog
- 多种视觉SLAM方案对比
- c语言图像对比度增强,图像对比度应用
- matlab车牌识别课程设计报告模板(附源代码)
- 下载Youtube-8m视频,ubuntu安装测试使用
- oracle恢复误删除数据/数据回滚
- Volatile关键字~转载自博客园的“海子”
- 用python把微信好友头像拼成一张图
- python中scale什么意思啊_python数字图像处理(7):图像的形变与缩放
- 实时系统vxWorks - 虚拟机环境搭建(修复)
- 欢迎观看Toni_hou的#生活1
- 固态硬盘与普通硬盘的区别
- 【智能制造】浅谈中国工业4.0的发展方向
- Qt 数据服务器、协议转换、系统托盘模块、CAN总线模块、反射内存卡模块
- 全球及中国CVD金刚石散热器行业研究分析及投资决策建议报告2022年版
热门文章
- 人类历史上有哪些逆天的文物?
- 如何建立自己的工作库,把分散信息有效整理?
- 栈在表达式求值中的应用
- php打印订单,WooCommerce: 打印订单
- mysql 查询缓存设置_MySQL查询缓存设置 提高MySQL查询性能
- sql server 事务_如何使用显式SQL Server事务回滚
- mysql按照列构建索引_列存储索引增强功能–在线和离线(重新)构建
- aws rds监控慢sql_如何使用Web控制台和AWS CLI停止AWS RDS SQL Server
- 报告正在使用哪些Reporting Services数据集字段?
- Kotlin-Note