LeetCode练题——53. Maximum Subarray

1、题目

53. Maximum Subarray——Easy

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

2、我的解法

(1)超时的暴力解法

 1 # -*- coding: utf-8 -*-
 2 # @Time    : 2020/2/6 22:15
 3 # @Author  : SmartCat0929
 4 # @Email   : [email protected]
 5 # @Link    : https://github.com/SmartCat0929
 6 # @Site    :
 7 # @File    : 53. Maximum Subarray(超时的暴力算法).py
 8 from typing import List
 9
10
11 class Solution:
12     def maxSubArray(self, nums: List[int]) -> int:
13         maxSum = -9999999999
14         if len(nums)>1:
15             maxSum = -99999
16             for i in range(0, len(nums)):
17                 for j in range(i, len(nums)):
18                     thisSum = sum(nums[i:j+1])
19                     if thisSum > maxSum:
20                         maxSum = thisSum
21             return maxSum
22         elif len(nums)==1:
23             maxSum=nums[0]
24             return maxSum
25         else:
26             return 0
27
28 print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))

(2)局部最大值(copy大神)

 1 # -*- coding: utf-8 -*-
 2 # @Time    : 2020/2/7 16:12
 3 # @Author  : SmartCat0929
 4 # @Email   : [email protected]
 5 # @Link    : https://github.com/SmartCat0929
 6 # @Site    :
 7 # @File    : 53. Maximum Subarray.py
 8
 9 from typing import List
10 class Solution:
11     def maxSubArray(self, nums: List[int]) -> int:
12         n = len(nums)
13         tmp = 0
14         res = float(‘-inf‘)
15         for i in range(n):
16             if tmp < 0:
17                 tmp = 0
18             tmp = tmp + nums[i]
19             res = max(res, tmp)
20         return res
21 print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))

原文地址:https://www.cnblogs.com/Smart-Cat/p/12274281.html

时间: 2024-07-30 09:17:52

LeetCode练题——53. Maximum Subarray的相关文章

LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

Description Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. F

41. leetcode 53. Maximum Subarray

53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6. 思路:这个题还挺经典

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum =

LeetCode练题——118. Pascal&#39;s Triangle

1.题目 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 2

[Leetcode][Python]53: Maximum Subarray

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 53: Maximum Subarrayhttps://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given t

LeetCode 53. Maximum Subarray(最大的子数组)

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. click to show more practice. Mor

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. click to show

【Divide and Conquer】53.Maximum Subarray(easy)

#week2# #from leetcode# Description Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum