[leetcode]Maximum Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-subarray/
题意:
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.
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:
# @param A, a list of integers
# @return an integer
def maxSubArray(self, A):
ThisSum = 0
MaxSum = -10000 for i in range( 0, len(A) ):
if ThisSum < 0:
ThisSum = 0
ThisSum = ThisSum + A[ i ]
MaxSum = max( ThisSum, MaxSum ) return MaxSum
[leetcode]Maximum Subarray @ Python的更多相关文章
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- 53. Maximum Subarray@python
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode——Maximum Subarray
Description: Find the contiguous subarray within an array (containing at least one number) which has ...
- 53. [LeetCode] Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
随机推荐
- Java 持久化之 -- IO 全面整理(看了绝不后悔)
目录: 一.java io 概述 什么是IO? IO包括输入流和输出流,输入流指的是将数据以字符或者字节形式读取到内存 分为字符输入流和字符输入流 输入流指的是从内存读取到外界 ,分为字符输入流和字节 ...
- fatal error C1060:compiler is out of heap space
今天svn update了下代码,rebuild工程的时候报错: fatal error C1060:compiler is out of heap space 意思是说编译器堆内存不足 百度结果:V ...
- 正睿OI 提高 Day1T3 ZYB玩字符串(DP)
题目链接 设可能的答案串为p,长为len.p一定是s的一个子串且len|n. 虽然一些p在s中可能被断成若干段,但删掉其中的若干段后,这段区间一定会被全部消掉. 于是枚举p后,可以用f[i][j]表示 ...
- 关于 TRegEx.Split()
表达式中的括号将严重影响分割结果. uses RegularExpressions; const FSourceText = '1: AAA 2: BBB 3: CCC'; // 分隔符将有三部分构成 ...
- ConcurrentHashMap之实现细节(转)
ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现.在这之前我对ConcurrentHashMap只有一些肤浅的理解,仅知道它采用了多个锁,大概也足够了 ...
- 百度地图api---实现新建地图
调用这个函数 function refresh() { history.go(0); } 实现了地图新建
- 通过NTP协议进行时间同步
最近发现手机的时间不是很准了,便到网上下了一个同步时间的小程序,简单了看了一下它的原理,是通过NTP协议来实现校时的,就顺便学习了一下NTP协议,用C#写了个简单的实现. NTP(Network Ti ...
- 使用newScheduledThreadPool来模拟心跳机制
(使用newScheduledThreadPool来模拟心跳机制) 1 public class HeartBeat { 2 public static void main(String[] args ...
- uistatusBar 详解
成功的方法: 方法1.隐藏应用程序内所有的StatusBar 第一步:在Info.plist,然后添加一个新的row,"View controller-based status bar ap ...
- 培养iOS开发新人的一个思路
坚持两个方法论: 1.发现问题的方法:(熟悉代码的过程) (1)照着一个完整的工程,从最基本的页面开始做起.不懂的地方就问,就查. (2)在阅读代码或拿到需求后要学会对问题进行分解.一个陌生的问题如果 ...