{ "type": "code_options", "author": "https://github.com/qiyuangong/leetcode", "source": "solution.md", "exercise_id": "a0d58370128f47f09f23c7af3c83d23d", "keywords": "数组,分治,动态规划", "title": "最大子序和", "desc": [ { "content": "\n

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

 

示例 1:

输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:
6
解释:
连续子数组 [4,-1,2,1] 的和最大,为 6 。

示例 2:

输入:nums = [1]
输出:
1

示例 3:

输入:nums = [0]
输出:
0

示例 4:

输入:nums = [-1]
输出:
-1

示例 5:

输入:nums = [-100000]
输出:
-100000

 

提示:

 

进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。

", "language": "markdown" } ], "answer": [ { "content": "", "language": "python" } ], "prepared": [ [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ] ], "template": { "content": "class Solution(object):\n\tdef maxSubArray(self, nums):\n\t\tmaxEndingHere = maxSofFar = nums[0]\n\t\tfor i in range(1, len(nums)):\n\t\t\tmaxEndingHere = max(maxEndingHere + nums[i], nums[i])\n\t\t\tmaxSofFar = max(maxEndingHere, maxSofFar)\n\t\treturn maxSofFar\n# %%\ns = Solution()\nprint(s.maxSubArray(nums = [-2,1,-3,4,-1,2,1,-5,4]))", "language": "python" }, "node_id": "dailycode-5bdf8ba742d641e29ecca718a5fcaab6", "license": "csdn.net", "created_at": 1637894161, "topic_link": "https://bbs.csdn.net/topics/600469821" }