41. Maximum Subarray/53. Maximum Subarray

  • 本题难度: Eas
  • Topic: Dynamic Programming

Description

Given an array of integers, find a contiguous subarray which has the largest sum.

Example
Example1:
Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.
Example2:
Given the array [1,2,3,4], the contiguous subarray [1,2,3,4] has the largest sum = 10.

Challenge
Can you do it in time complexity O(n)?

Notice
The subarray should contain at least one number.

我的代码

class Solution:def maxSubArray(self, nums):""":type nums: List[int]:rtype: int"""#Oct 10, 2018l = len(nums)if l == 0:return 0if l == 1:return nums[0]top = nums[0]tmp = topfor i in range(1,l):if top<0 and nums[i]>top:top = nums[i]tmp = topcontinueif nums[i]+tmp>0:tmp = tmp + nums[i]if tmp>top:top = tmpelse:tmp = 0return top

别人的代码

for i in range(1, len(nums)):if nums[i-1] > 0:nums[i] += nums[i-1]return max(nums)

思路
那个别人的代码真个天才我的天哦

转载于:https://www.cnblogs.com/siriusli/p/10388349.html

[Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray相关推荐

  1. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  2. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  3. LeetCode(53):Maximum Subarray

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  4. LeetCode 53. Maximum Subarray--动态规划--C++,Python解法

    题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...

  5. [LeetCode]: 53: Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  6. leetCode 53. maximum subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. LeetCode#53 Maximum Subarray

    Problem Difinition: Find the contiguous subarray within an array (containing at least one number) wh ...

  8. C#解leetcode 53.Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. Leetcode 53. Maximum SubarrayJAVA语言

    1 2 3 Find the contiguous subarray within an array (containing at least one number) which has the la ...

最新文章

  1. 今天,我辞去了亚马逊年薪 50 万美金的工作!
  2. 系统重构的10点经验总结
  3. Mybatis核心配置文件SqlMapConfig.xml独立使用配置内容
  4. mysql insert 运算_MySql insert插入操作的3个小技巧分享
  5. Java使用Cipher类实现加密,包括DES,DES3,AES和RSA加密
  6. Spring知识点总结
  7. 【算法】算法之会议安排问题(C++源码)
  8. oracle校验统一社会信用代码函数
  9. 把一个godaddy的域名转回国内的注册商
  10. 【数据库架构】NewSQL和PGXC
  11. Android/安卓 文本添加中划线、下划线的方法
  12. 什么是php 开发,php开发是什么?
  13. 微信开发 现金红包、裂变红包、企业付款
  14. nyoj 一笔画问题
  15. 第19章 Linux电源管理的系统架构和驱动之CPUFreq驱动
  16. 关于SQL注入,绕过逗号过滤
  17. C# 原子操作 Interlocked
  18. ck3m多轴运动控制器笔记1:电机的配置与注意事项
  19. 2022-2028年中国高速铁路行业投资分析及前景预测报告
  20. stm32指纹、蓝牙识别门禁系统项目详解

热门文章

  1. 开源项目托管GitHub的使用详述
  2. C语言核心技术——函数
  3. Spring 声明式事务在业务开发中容易碰到的坑总结
  4. MySQL概要——《深究MySQL》
  5. 含类定义的完整python程序_Python——变量,运算,条件,循环
  6. centos解压zip命令_2、centos下安装elasticsearch-head
  7. java练手代码大全手机版_java循环练习的简单代码实例
  8. 【网工必备】网络端口号大全......
  9. 实际场景中,云原生存储面临的 7 个挑战
  10. 鸿蒙2.0升级计划名单,鸿蒙OS 2.0适配 计划-升级名单