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.

思路


这道题看到之后第一想到的就是使用动态规划来解决这个问题。使用动态规划需要申请一个辅助数组,另外还需要动态方程,方程为dp[i] = nums[i] + ( dp[i-1] if dp[i-1] > 0 else 0)。 这种解法的时间复杂度为O(n),空间复杂度为O(n)。

  第二种思路就是我们设置一个sum_标志量和结果变量,然后从头遍历,使用sum_变量存储连续数组的和,如果当前小于0直接赋值为0。最后返回结果变量。时间复杂度为O(n),空间复杂度为O(1)。

第一种思路代码


 class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) < 1 :
return 0
dp = [0] * len(nums) # 辅助数组
dp[0] = nums[0] # 记录nums第一个的值
max_num = dp[0] # 记录子数组最大的值
for i in range(1, len(nums)):
dp[i] = nums[i] + (dp[i-1] if dp[i-1]> 0 else 0) # 记录当前最大的子数组和的值
max_num = max(max_num, dp[i])
return max_num

第二种思路解决办法


 class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) < 1 :
return 0
res, sum_ = nums[0], 0
for i in nums:
sum_ += i
res = max(sum_, res)
if sum_ < 0:
sum_ = 0
return res

【LeetCode每天一题】Maximum Subarray(最大子数组)的更多相关文章

  1. [LeetCode] Maximum Subarray 最大子数组

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

  2. [LeetCode] 53. Maximum Subarray 最大子数组

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

  3. [leetcode]53. Maximum Subarray最大子数组和

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

  4. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

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

  5. [LintCode] Maximum Subarray 最大子数组

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

  6. Maximum Subarray(最大子数组)

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

  7. 【js】Leetcode每日一题-解码异或后数组

    [js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...

  8. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

  9. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

随机推荐

  1. jQuery雷达扫描切换幻灯片代码

    基于jQuery雷达扫描切换幻灯片代码.这是一款切换效果类似雷达扫描,支持鼠标滚轮滚动切换.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=" ...

  2. 【交换机】交换机RLDP(环路检测&链路检测)功能介绍及配置说明

    功能简介RLDP 全称是Rapid Link Detection Protocol,是锐捷网络自主开发的一个用于快速检测以太网链路故障的链路协议.一般的以太网链路检测机制都只是利用物理连接的状态,通过 ...

  3. Python threading 多参数传递方法

    今天开启线程传递参数的时候,出现了一个小问题,一直不能传递多个参数,如下 import threading thread1 = threading.Thread(target=fun, args=[1 ...

  4. mvc webapi路由重写

    修改app_start/webapiconfig.cs using System.Web.Http; using System.Web.Routing; using Ninject; using Tx ...

  5. .net IIS MVC Rest api 跨域 PUT DELETE 404 无法使用问题解决方案

    一.WebConfig配置法(system.webServer 重点是 httpProtocol handlers) http://www.jinxuliang.com/blog/article/re ...

  6. 误删除libc.so.6的解决

    最近安装一个软件需要glibc-2.17. 使用ldd --version 发现系统的glibc版本为 glibc-2.12,当时没有想到更好的方法,就尝试将系统的glibc版本修改为glibc-2. ...

  7. SQUAD的rnet复现踩坑记

    在港科大rnet(https://github.com/HKUST-KnowComp/R-Net) 实现的基础上做了复现 采用melt框架训练,原因是港科大实现在工程上不是很完美,包括固定了batch ...

  8. java发送邮件无法显示图片 图裂 的解决办法

    package com.thinkgem.jeesite.common.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...

  9. 《转》循环神经网络(RNN, Recurrent Neural Networks)学习笔记:基础理论

    转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...

  10. java高级---->Java观察者的原理

    观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,让他们能够自动更新自己.今天我们通过模拟按钮的处理事件来深入Java ...