[LeetCode&Python] Problem 53. Maximum Subarray
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.
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxsum=nums[0]
n=len(nums)
for i in range(1,n):
nums[i]=max(nums[i],nums[i]+nums[i-1])
maxsum=max(maxsum,nums[i])
return maxsum
[LeetCode&Python] Problem 53. Maximum Subarray的更多相关文章
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- [LeetCode&Python] Problem 628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode&Python] Problem 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode&Python] Problem 559. Maximum Depth of N-ary Tree
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 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 ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
随机推荐
- 从安装Mac OS X虚拟机到第一个IOS程序
对于纯粹地抄这种行为是比较抵触的,别人已经写得挺好的东西没必要又去写一遍,但如果不写经验来看下次再做时自己又要重复百度筛选一遍,所以还是要记一记. 之前要获取IOS静态库的版本,但一直以来没有Mac没 ...
- react native练习
import React, { Component } from 'react' import { Platform, StyleSheet, Text, View,Image ,FlatList} ...
- 学习笔记------------解决margin塌陷
首先来解释一下什么是marg塌陷? 父子嵌套元素垂直方向的margin,父子元素是结合在一起的,他们两个会取其中最大的值 正常情况下应该是父级元素相对于浏览器定位,而子级元素相对于父级元素定位 但是m ...
- AngelToken——富有价值的区块链服务平台
关于我们 Angel Token,简称ANG,是基于ETH代币的去中心化数字交易平台. 行业现状 截至2017年12月,全球数字货币总市值已经触及6000亿美元.而2016年12月31日,这个数字才仅 ...
- ES6 memo
一.热门问题 介绍redux,主要解决什么问题 是管理应用程序状态的库,解决数据管理和数据通信的问题 Promise.Async有什么区别 Async 更简洁,不需要用 then 连接 Promise ...
- RPA与AI_新技术能给企业业务流程带来怎样的价值?
RPA助力于流程自动化蜕变 RPA是Robotic Process Automation的缩写,意为: 将机器人作为虚拟劳动力,依照预先设定的程序与现有用户系统进行交互并完成设定好的任务流程.RPA可 ...
- BigDecimal类的用法
(一)BigDecimal类的常用的几个构造方法 BigDecimal(int):将int表示形式转换为BigDecimal对象 BigDecimal(String):将字符串表示形式转换为BigDe ...
- java——类、对象、方法
一.类 1.Java语言把一组对象中相同属性和方法抽象到一个Java源文件就形成了类. 一个java文件可以有多个类,但是每一个类都会生成一个class字节码文件. 如果class 前加public ...
- node.js学习6---第三方依赖(模块或者说是包)的导入 npm 以及 cnpm命令的使用
npm命令用于导入node.js的第三方包,相当于java中使用maven来导入第三方依赖: 1.npm init -y 命令:在命令窗口中执行后,会出现如下的json文件: 右边记录了安装的第三方包 ...
- Linux监控
第三十次课 Linux监控 目录 一. Linux监控平台介绍 二. zabbix监控介绍 三. 安装zabbix 四. 忘记Admin密码如何做 五. 主动模式和被动模式 六. 添加监控主机 七. ...