[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] 的和最大,为 ...
随机推荐
- html ul 里 能放其他标签吗,比如h3、p?
按标准的话,ul 里面应该只能放li,不过看见同事有的放了h3.p这些,这样到底可不可以? <ul>里面只能放<li>.但是<li>里面可以放<h*> ...
- 微课程--Android--Activity组建与Intent
安卓的四大基本组件 Activity是经常使用的组件 1 展示用户界面 2 响应用户操作 Service 1 在后台长时间运行 2 没有用户界面 ContentProvider 1 管理和共享应用数据 ...
- [转] - C++程序启动过程
先说编译.链接过程1.预编译展开宏2.为每一个.cxx源文件编译一个目标文件3.编译器合成这些目标文件成一个库文件,同时解析可以找到的符号引用4.连接器把目标的库文件和所需要的引用的静.动态链接库进行 ...
- ASP中可能出现的一种包含漏洞(Server.execute)
author: bin <% Server.execute(request(“file”)) %> 与include的区别,它可以动态包含文件. 被包含文件里面可执行ASP代码,在国外的源 ...
- log4net配置文件设置
windows服务执行cmd命令 最长公共子字符串 log4net配置文件设置 2011-11-16 13:15:41| 分类: Notes | 标签: |字号大中小 订阅 log4net ...
- GC、LOH和Performance相关
Performance Now that we have a basic model for how things are working, let's consider some things th ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- MarkDown编辑器用法
代码行1 var s = "JavaScript syntax highlighting"; alert(s); 代码行2:ESC下面的英文3个波浪号~按键 var s = &qu ...
- 【ZZ】 DShader之位移贴图(Displacement Mapping)
http://www.myexception.cn/other/1397638.html DShader之位移贴图(Displacement Mapping) www.MyException.Cn ...
- JS页面跳转 神器! window.href window.open
window.open 打开页面, 网址为新的网址 window.href 为打开本网址的链接, 为网站URL+传入的URL