题解 | #连续子链表最大和#

连续子链表最大和

http://www.nowcoder.com/practice/650b68dfa69d492d92645aecd7da9b21

利用动态规划的思想,最大子链表的和只与之前子链表+当前值 和 当前值有关,并用一个全局值记录整个过程中出现的最大值

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return int整型
#
class Solution:
    def FindGreatestSumOfSubArray(self , head: ListNode) -> int:
        # write code here
        if not head: return 0
        if not head.next:return head.val
        res = cur = float("-inf")
        while head:
            cur = max(cur + head.val, head.val)
            res = max(res, cur)
            head = head.next
        return res
全部评论

相关推荐

12-18 19:36
已编辑
门头沟学院 Java
程序员牛肉:可以的,简历没毛病了。 虽然还是偏向同质化,不过学历不错。后续我觉得重心放到刷实习+摆脱同质化问题上
实习简历求拷打
点赞 评论 收藏
分享
饿魔:看到在线简历了吧
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务