[LeetCode]题解(python):053-Maximum Subarray
题目来源
https://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
.
题意分析
Input: a list
Output: a number
Conditions:求最大连续子序列的和
题目思路
这题有点像动态规划,但是发现不需要索引之类的,所以想直接知道以某个值为结尾的最大子串的和,用d[]来存储该和,然后遍历一次即可,注意用max值来保存所需,不断更新max值
做完之后发现其实没必要用一个数组保存所有的和,直接用另一个变量保存前一个位置的最大和即可,代码是用list保存的没有改进= =
AC代码(Python)
__author__ = 'YE' class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
if length == 0:
return 0
if length == 1:
return nums[0] d = [0 for i in range(length)] d[0] = nums[0]
max = d[0] for i in range(1, length):
d[i] = nums[i] + d[i - 1] if (nums[i] + d[i - 1]) > nums[i] else nums[i]
if d[i] > max:
max = d[i] return max s = Solution()
nums = [-2,1,-3,4,-1,2,1,-5, 4]
print(s.maxSubArray(nums))
[LeetCode]题解(python):053-Maximum Subarray的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- Java for LeetCode 053 Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式
原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...
- LeetCode之“动态规划”:Maximum Subarray
题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 053 Maximum Subarray 最大子序和
给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...
随机推荐
- COJ975 WZJ的数据结构(负二十五)
试题描述 输入一个字符串S,回答Q次问题,给你l,r,输出子序列[l,r]的最长连续回文串长度. 输入 第一行为一个字符串S. 第二行为一个正整数Q. 接下来Q行每行为l,r. 输出 对于每个询问,输 ...
- COJ986 WZJ的数据结构(负十四)
WZJ的数据结构(负十四) 难度级别:D: 运行时间限制:6000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小 ...
- ARC指南2 - ARC的开启和禁止
要想将非ARC的代码转换为ARC的代码,大概有2种方式: 1.使用Xcode的自动转换工具 2.手动设置某些文件支持ARC 一.Xcode的自动转换工具 Xcode带了一个自动转换工具,可以将旧的源代 ...
- iOS开发项目之二 [ App appicon与启动图]
*appicon尺寸 *不是所有的appicon都是需要配置 *尽量不要透明--(透明的话,会在桌面显示成背景黑色) 1 如果没有配置5s的启动图,回去往下找,找到4s之后,会把界面以4s启动图的大小 ...
- 图解classloader加载class的流程及自定义ClassLoader
图解classloader加载class的流程及自定义ClassLoader 博客分类: JVM JavaJVM虚拟机EXTSUN /** * 转载请注明作者longdick http://l ...
- 如何快速查找IP归属地
这两天遇到这么一个问题,就是查找一个IP的归属地.当然我会有一个IP段的分配列表,格式如下: 16777472 16778239 XX省 XX市 第一列是IP段的起始IP,第二列是IP段的 ...
- C++ Generate Rand Number Array by "srand()" 生成随机数
在C++中,我们有时想生成一个由随机数组成的数组,而且随机数的范围也可由我们来设定.那么我们就要用到srand()函数配合rand()来使用,参见如下代码: #include <vector&g ...
- 你们以为运营商只是HTTP插点广告而已么?
国内某邮件服务商,近期在某南方地区有大量客户反应登录时出错和异常,于是工作人员进行了一下跟进,发现如下: 首先,邮件服务商登陆页面为普通HTTP协议发送,提交时通过JS进行RSA加密(没错,JS的RS ...
- shell.application asp多种组件执行cmd 单文件版本
<%@ Language="VBScript" %> <% ) theComponent() = "Scripting.FileSystemObject ...
- app.config中的connectionstring
<connectionStrings> <add name="wz" connectionString="server=www.junjv.com ...