Given an array having positive integers, find a continous subarray which adds to a given number.
-----------------------------------------------
Take notice that all numbers are positive!
It
can be done O(n).
My logic is as follows:- Keep adding elements to a running sum till it is either equal to given number or is greater than the given number. As soon as
we add a number and the running number is greater than the given number we subtract the elements from the beginning of the array till it is again less than the given number. After the current sum is less than the given number we add the next element and check
and continue the same process.

本文介绍了一种在正整数数组中找到连续子数组使其和等于给定数值的方法。该算法采用一次遍历的方式,通过调整运行总和来确保效率达到O(n)级别。
1万+

被折叠的 条评论
为什么被折叠?



