LeetCode练题——53. Maximum Subarray
1、题目
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)超时的暴力解法
# -*- coding: utf-8 -*-
# @Time : 2020/2/6 22:15
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 53. Maximum Subarray(超时的暴力算法).py
from typing import List class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxSum = -9999999999
if len(nums)>1:
maxSum = -99999
for i in range(0, len(nums)):
for j in range(i, len(nums)):
thisSum = sum(nums[i:j+1])
if thisSum > maxSum:
maxSum = thisSum
return maxSum
elif len(nums)==1:
maxSum=nums[0]
return maxSum
else:
return 0 print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))
(2)局部最大值(copy大神)
# -*- coding: utf-8 -*-
# @Time : 2020/2/7 16:12
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 53. Maximum Subarray.py from typing import List
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
tmp = 0
res = float('-inf')
for i in range(n):
if tmp < 0:
tmp = 0
tmp = tmp + nums[i]
res = max(res, tmp)
return res
print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))
LeetCode练题——53. Maximum Subarray的更多相关文章
- 刷题53. Maximum Subarray
一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- [LeetCode&Python] Problem 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
随机推荐
- Tomcat创建项目
查看项目信息 index.jsp默认首页 更新资源自动部署不用重启服务器,要用debug的方式启动 更新java代码和更新资源自动部署不用重启服务器,要用debug的方式启动
- #! /usr/bin node 和#! /usr/bin/env node两者的区别
是Unix和Linux脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它 !/usr/bin/node是告诉操作系统执行这个脚本的时候,调用/usr/bin下的node ...
- Python的DataFrame遍历_转CSDN_J小白Y
转CSDN_J小白Y:https://blog.csdn.net/Jarry_cm/article/details/99683788 1.DataFrame.iterrows() 返回{索引,Seri ...
- ES6常用语法,面试应急专用!
常用的ES6语法 注:该文章为转载,原地址为https://www.jianshu.com/p/fb019d7e8b15 什么是ES6? ECMAScript 6(以下简称ES6)是JavaScr ...
- 传奇装备锻造升级UPGRADEITEMEX功能详解
装备升级功能可以指定升级物品及属性,按指定机率得到结果.需要升级的装备物品必须放在身上.命令格式UPGRADEITEMEX 物品位置(0-12) 属性位置(0-14) 成功机率(0-100) 点数机率 ...
- itest(爱测试) 4.3.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
4.3.0 发布后有三个用户强烈要求的更新,所以一周后4.3.1出炉,有点版本帝的味道哈,用户的反馈是我们持续升级的动力...... itest 简介:查看简介 test 开源敏捷测试管理,testO ...
- hadoop3.1.1 HA高可用分布式集群安装部署
1.环境介绍 涉及到软件下载地址:https://pan.baidu.com/s/1hpcXUSJe85EsU9ara48MsQ 服务器:CentOS 6.8 其中:2 台 namenode.3 台 ...
- 201771010135-杨蓉庆 实验一 软件工程准备—用Markdown写构建之法
项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh 博客园 https://www.cnblogs.com/nwnu-daizh/p/12369881.h ...
- Bug搬运工-Forerunner CRC error on 54SG/53SG3 triggers watchdog timeout crash
这个bug,一般是设备4948 Crash的情况: 标志1:Error Message C4K_SUPERVISOR-2-SOFTERROR: memory inconsistency detecte ...
- SpringMVC的三种处理器适配器
SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping.SimpleControllerHandlerAdapter.ControllerClassNam ...