HackerRank——max-sum-subarray(最大子序列和)

给定一个包含整数的数组,找到一个没有0的连续子数组,返回其最大和。示例输入为5个整数3、4、0、1、2,输出为7,因为子数组[3, 4]的最大和为7。" 121873172,11731308,Android自定义Application与数据传递及缓存,"['移动开发', 'android', '内存管理']
摘要由CSDN通过智能技术生成

You are given an array of integers.
You need to find the contiguous subarray of the maximum sum in . The subarray should not contain the number . Please find the maximum sum that is possible.

Note 1: The subarray can also be empty.
Note 2: The answer will fit in 32 bit-signed integer.

Input Format

The first line contains the integer . The next line contains integers representing the numbers in the array.

Constraints

Output Format

Output a single line representing the maximum sum that can be obtained.

Sample Input

5
3 4 0 1 2
Sample Output

7
Explanation

The subarray with the maximum sum that doesn’t contain a is .
Hence, the sum is.

题目大意:

求不包括元素0的最大子序列和。

题目解析:

遍历序列,当遍历到第i个元素时,判断前面的连续子序列的和是否大于0,有就加上;遍历到0元素时,自动清零。

具体代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值