数据结构算法操作试题(C++/Python)——最大子序和

时间:2022-07-24
本文章向大家介绍数据结构算法操作试题(C++/Python)——最大子序和,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/maximum-subarray/

2. 解答

python: 32ms, 11.2, 93%

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        sum = 0
        max_sub_sum = nums[0]
        for num in nums:
            sum += num
            if sum > max_sub_sum:
                max_sub_sum = sum
            if sum < 0:
                sum = 0
        return max_sub_sum

其他方法看 leetcode 链接 评论区~