【LintCode 简单】41. 最大子数组
2018-01-19 14:57
344 查看
1.问题描述:
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
注意事项
子数组最少包含一个数
2.样例:
给出数组
3.代码:class Solution:
"""
@param: nums: A list of integers
@return: A integer indicate the sum of max subarray
"""
def maxSubArray(self, nums):
# write your code here
length=len(nums)
sum=0
ans=-10000
for i in range(length):
sum+=nums[i]
if sum>ans:
ans=sum
if sum<0:
sum=0
return ans
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
注意事项
子数组最少包含一个数
2.样例:
给出数组
[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为
[4,−1,2,1],其最大和为
6
3.代码:class Solution:
"""
@param: nums: A list of integers
@return: A integer indicate the sum of max subarray
"""
def maxSubArray(self, nums):
# write your code here
length=len(nums)
sum=0
ans=-10000
for i in range(length):
sum+=nums[i]
if sum>ans:
ans=sum
if sum<0:
sum=0
return ans
相关文章推荐
- [LintCode]41.最大子数组
- LintCode【简单】41. 最大子数组 。代码及思路
- python_lintcode_简单题 _44最小子数组_41最大子数组
- LintCode-剑指Offer-(41)最大子数组
- LintCode-41.最大子数组
- LintCode 41. 最大子数组
- LintCode之41 最大子数组
- LintCode 41 最大子数组
- lintcode :最大子数组
- lintcode 41.最大子数组
- Lintcode 41.最大子数组
- [原创]java WEB学习笔记41:简单标签之带属性的自定义标签(输出指定文件,计算并输出两个数的最大值 demo)
- LintCode-最大子数组
- LintCode-41. 最大子数组
- 【LintCode】最大子数组
- LintCode【简单】97. 二叉树的最大深度 。代码及思路
- [Lintcode] #41 最大子数组
- 41. 最大子数组
- lintcode,最大子数组
- 【简单】Lintcode 41:Maximum Subarray