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.

 class Solution(object):
def maxSubArray(self, nums):
thissum,maxsum = 0,-9999999999999
for v in nums:
thissum += v
if thissum>maxsum:
maxsum = thissum
if thissum<0:
thissum = 0
return maxsum
 class Solution(object):
def maxSubArray(self, nums):
thissum=maxsum = nums[0]
for v in nums[1:]:
thissum = max(v,v+thissum)
maxsum = max(maxsum,thissum)
return maxsum

[leetcode DP]53. Maximum Subarray的更多相关文章

  1. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  2. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  3. 【LeetCode】53. Maximum Subarray (2 solutions)

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

  4. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  5. 【一天一道LeetCode】#53. Maximum Subarray

    一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...

  6. Leetcode No.53 Maximum Subarray(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums, find the contiguous subarray (containing at least one nu ...

  7. LeetCode OJ 53. Maximum Subarray

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

  8. 【Leetcode】53. Maximum Subarray

    题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...

  9. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

随机推荐

  1. Uva 11549 - Calculator Conundrum 找规律加map

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. sql 通过分数字段倒排获取名次的方法

    row_number() over(order by sort desc) 应用场景: 比如学员成绩表中有userid,username,sorce登字段,需要取出学员成绩的名次:表中没有名次字段,只 ...

  3. sql 存储过程导出指定数据到.txt文件(定时)

    需求:每天生成一份txt文件数据,供第三方通过http方式调用 方法: 1.新建存储过程: USE [LocojoyMicroMessage] GO /****** Object: StoredPro ...

  4. git 配置 SSH密钥

    1.登录用户 $ git config --global user.name "geekfeier" $ git config --global user.email " ...

  5. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  6. linux设备驱动之USB主机控制器驱动分析 【转】

    转自:http://blog.chinaunix.net/uid-20543183-id-1930831.html   ---------------------------------------- ...

  7. kettle简单插入与更新

    Kettle简介:Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.Kettle 中文名称叫水壶,该项目的主程序员MATT 希望 ...

  8. MySQL字符集编码相关

    Windows 10家庭中文版,MySQL  5.7.20,2018-05-07 Part.1 查找数据库的字符集编码 查看MySQL字符集编码:status命令 使用命令行登录MySQL服务器,然后 ...

  9. jQuery中绑定事件的几种方法

    以click事件为例,jQuery中绑定事件有三种方法: (1)target.click(function(){});  (2)target.bind("click",functi ...

  10. tensorflow中的boolean_mask

    将mask中所有为true的抽取出来,放到一起,这里从n维降到1维度 tensor = [[1, 2], [3, 4], [5, 6]] import numpy as np mask=np.arra ...