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.
Example
Given [-3, 1, 2, -3, 4]
, return [0, 2]
or [1, 3]
.
Note
There is at least one subarray that it's sum equals to zero.
vector<int> subarraySum(vector<int> nums){ unordered_map<int, int> map; map[0] = -1; int sum = 0; vector<int> res(2); for(int i=0; i<nums.size(); i++) { sum += nums[i]; if(map.count(sum) != 0) { res[0] = map[sum]+1; res[1] = i; return res; } map[sum] = i; } return res; }
相关推荐
matlab开发-subarray。从数组中提取子数组。用于功能输出。
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的子数组。如果存在该子数组返回true,否则返回false。 #include #include using namespace std; int main() { std::cout <... subset[i
最大子数组总和maximum subarray sum使用Java语言中的所有复杂度来计算子数组的最大和。 1.O(n ^ 3) 2.O(n ^ 2) 3.O(n)
easy_Maximum-Subarray 提交链接 / Submit (You need register/login first before submit.) (在提交前你需要先注册或登录) 题目描述 / Description Given an integer array nums, find the contiguous subarray ...
npm install max-subarray bower install max-subarray 用法 const maxSubarray = require ( 'max-subarray' ) ; console . log ( maxSubarray ( [ 1 , - 4 , 1 , 3 , 6 , - 2 , - 9 ] ) ) ; // [1, 3, 6] console ....
import SubarrayProxy from 'ember-subarray-proxy' ; var slice = SubarrayProxy . create ( { content : Ember . A ( [ 'a' , 'b' , 'c' ] ) , limit : 2 } ) ; slice . get ( 'length' ) // 2 slice . get ( '...
子阵列划分的root-music algorithm
最大子数组总和问题给定一个整数数组,找到一个具有最大和的序列。 看一个例子: let array = [ 1 , - 1 , 5 , 3 , - 7 , 4 , 5 , 6 , - 100 , 4 ]function largestSubarraySum ( array ) { // code to write here}...
最大子数组总和 问题 给定一个整数数组,找到一个具有最大和的序列。 例如,看下面的例子。 let array = [ 1 , - 1 , 5 , 3 , - 7 , 4 , 5 , 6 , - 100 , 4 ] function largestSubarraySum ( array ) { ...
leetcode 2 code_interview leetcode和lintcode的一些题目的解法 是剑指offer 是面试中遇到的有意思的题目总结,...138-Subarray-Sum integer-arr 整型数组 值得回顾的题 41-first-missing-positive 01-Two-Sum 求解第K
希望更多的人能和我一样,把本狂想曲系列中的任何一道面试题当做一道简单的编程题或一个实质性的问题来看待,在阅读本狂想曲系列的过程中,希望你能尽量暂时放下所有有关面试的一切包袱,潜心攻克每一道“编程题”,...
Longest Subarray Length小程序测试根据给定的k计算最长子数组长度
number of subarray whose sum is S using dynamic programming
最大子数组问题Kadane算法的一些微小实现,以各种语言(C,C ++,python,Java)解决了最大子数组问题。 它从标准输入(即从命令行或文本文件)获取数组。
lru cache leetcode leetcode 记录自己刷leetcode时...subarray-sum-equals-k binary-tree-preorder-traversal n-queens-ii populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...
收集面试中提出的一些重要问题。 数据结构算法面试问题面试中提出的一些重要问题的集合。...数组https://leetcode.com/problems/3sum/ [解决方案] https://leetcode.com/problems/trapping-rain-water/ [解决方案] ...
leetcode双人赛 leetcode-solution 闲暇之余,刷一下题,弥补数据结构和算法的短板 ...maximum-subarray 加一 plus-one 合并两个有序数组 merge-sorted-array 杨辉三角 pascals-triangle 杨辉三角 II pa
LeetCode209_Minimum_Size_Subarray_Sum 给定一个整型数组和一个数字s,找到数组中最短的一个连续子数组, 使得连续子数组的数字和sum>=s,返回这个最短的连续子数组的长度值, 如:给定数组[2,3,1,2,4,3],s=7 答案为...
最大子序列和问题(Maximum Subarray Sum Problem)是求解一个数组中连续子数组的和的最大值的问题。