题目描述:

方法一:O(N)

class Solution(object):
def maxSubArray(self, nums):
sum = 0
max_sub_sum = nums[0]
for num in nums:
sum += num
if sum > max_sub_sum:
max_sub_sum = sum
if sum < 0:
sum = 0
return max_sub_sum

二:O(N)/O(1)

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

三:分治法 O(logn) O(logn)

class Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length <= 0)
return 0;
int len = nums.length;
return getInfo(nums,0,len - 1).mSum;
}
class wtevTree{
int lSum;
int rSum;
int iSum;
int mSum; wtevTree(int l, int r, int i, int m){
lSum = l;
rSum = r;
iSum = i;
mSum = m;
}
}
wtevTree pushUp(wtevTree leftT,wtevTree rightT){
int l = Math.max(leftT.lSum,leftT.iSum + rightT.lSum);
int r = Math.max(leftT.rSum + rightT.iSum,rightT.rSum);
int i = leftT.iSum + rightT.iSum;
int m = Math.max(leftT.rSum + rightT.lSum,Math.max(leftT.mSum,rightT.mSum));
return new wtevTree(l,r,i,m);
} wtevTree getInfo(int[] nums,int left,int right){
if(left == right)
return new wtevTree(nums[left],nums[left],nums[left],nums[left]);
int mid = (left + right) >> 1;
wtevTree leftT = getInfo(nums,left,mid);
wtevTree rightT = getInfo(nums,mid + 1,right);
return pushUp(leftT,rightT);
}
}

leetcood学习笔记-53*-最大子列和的更多相关文章

  1. [原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. leetcood学习笔记-168-excel表列名称

    题目描述: 方法一:asiic码 class Solution: def convertToTitle(self, n: int) -> str: if (n-1)//26 == 0: retu ...

  3. leetcood学习笔记-171-excel表列序号

    题目描述: 方法: class Solution: def titleToNumber(self, s: str) -> int: num = 0 r = 1 for i in s[::-1]: ...

  4. DataTable学习笔记---排序细则、列隐藏

    耽误了好几天,因为要做一个嵌入式的实验-android内核编译与裁剪,很久之前装的wubi不知道为什么运行出错了,然后看着当前的win7系统觉得有点讨厌了,也是因为快1年半没装机了,所以就重新装机了, ...

  5. Linux下汇编语言学习笔记53 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  6. MySQL学习笔记:新增一列

    1.在一个已建好的表中,最后一列位置添加一列,可使用: ALTER TABLE aa_numbers_small ADD COLUMN date_time DATE NOT NULL; 2.添加一列到 ...

  7. leetcood学习笔记-20

    python字符串与列表的相互转换   学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.s ...

  8. leetcood学习笔记-14*-最长公共前缀

    笔记: python if not   判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不 ...

  9. leetcood学习笔记-54-螺旋矩阵

    题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0 ...

随机推荐

  1. 微信小程序开发之https服务器搭建三步曲

    本篇文章主要讲述3个方面的内容,如下: 1.SSL证书的获取 2.服务器 Nginx SSL 证书的配置. 3.如何兼容80端口和443端口以及为什么要同时兼容这两个端口. 1.SSL证书的获取 ht ...

  2. unicode_stop - 撤销控制台unicode模式(例如, 回到8-bit模式).

    总览 unicode_stop 描述 unicode_stop 撤销以前 unicode_start(1) 命令的效果, 将显示屏和键盘设回到 8-bit 模式.

  3. etimer

     Contiki包含一个时钟模型和5个定时器模型(timer, stimer, ctimer, etimer, and rtimer),先学习etimer吧. etimer是一个结构体,(个人用eve ...

  4. 笔记45 Hibernate快速入门(二)

    Hibernate O/R 映射 一.多对一 一个Product对应一个Category,一个Category对应多个Product,所以Product和Category是多对一的关系.使用hiber ...

  5. Nginx集群配置启动报错

  6. C++ 数组作为参数的传递

    //#include <iostream> //#include <conio.h> //using namespace std; // // //void are7(int( ...

  7. Ruby 安装 – Unix

    Ruby 安装 - Unix 下面列出了在 Unix 机器上安装 Ruby 的步骤. 注意:在安装之前,请确保您有 root 权限. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 Ruby ...

  8. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)

    题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...

  9. (转)mysql分区技术2

    转:http://database.51cto.com/art/201002/184392.htm 非整数列分区 任何使用过分区的人应该都遇到过不少问题,特别是面对非整数列分区时,MySQL 5.1只 ...

  10. web应用本质

    web应用的本质 在之前学习的socket网络编程中,是基于: 架构:C/S架构 协议:TCP/UDP协议 运行在OSI七层模型中的传输层 那么在web应用中,是基于: 架构:B/S架构 协议:Htt ...