53. Maximum Subarray@python
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
题目地址: Maximum Subarray
难度: Easy
思路: 最大子序列和问题
代码:
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
tmp = 0
res = float('-inf')
for i in range(n):
if tmp < 0:
tmp = 0
tmp = tmp + nums[i]
res = max(res, tmp)
return res
时间复杂度: O(n)
空间复杂度: O(1)
53. Maximum Subarray@python的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- 原生js 的ajax封装
/** * 封装ajax函数(包括跨域) * @method ajax * @param option :{type:"post" or "get" 请求方式, ...
- IT兄弟连 JavaWeb教程 AJAX以及JSON字符串经典案例
案例需求:客户端发送AJAX请求服务器端获取用户信息的数据. 案例实现: 在服务器端要将Java对象转换成JSON字符串,如果使用拼接JSON字符串的方式非常繁琐,并且非常容易出错,所以一般会借助第三 ...
- NOIp2013 火柴排队【逆序对/思维】 By cellur925
题目大意:给你两列数\(ai\)和\(bi\),你可以交换每列数中相邻的两个数,求一个最小交换次数使\(\sum_{i=1}^{n}(a_i-b_i)^2\) 最小. 最后满足条件的两个序列一定是各个 ...
- Leetcode:环形链表2
题目 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 解答 这道题真的很巧妙,首先我们有了环形链表1这道题的铺垫,就能方便的判断有无环了,但是题目要求我们找到环形链表的 ...
- Uva11572
读入可以不需要存入数组 #include<bits/stdc++.h> #define inf 0x3f3f3f3f //const int maxn=; using namespace ...
- Hive进阶_汇总
=========================================================================== 第2章 Hive数据的导入 使用Load语句执行 ...
- ADC中的滤波算法
STM32的AD最大输入时钟不超过14MHZ,最高采样速度1us,可以采用DMA或者内部的基本定时器/高级定时器来触发,利用模拟看门狗监控所选择的的所有通道,如果超过模拟的 阀[fá] 值,将产生中断 ...
- Jenkins+Gitlab+Ansible自动化部署(三)
接Jenkins+Gitlab+Ansible自动化部署(一)https://www.cnblogs.com/zd520pyx1314/p/10210727.html 和(二)https://www. ...
- Dell服务器安装系统中遇到的坑
在本学期开学初期,由于后续实验的需要,老师为我们配置了服务器,该服务器的型号为Dell Power R730. 由于我也是一个小白,在服务器安装系统的过程中,遇到了一些麻烦,在这里记录下来,希望自己能 ...
- 洛谷P1057 传球游戏
f[i][j]表示第i轮j拿到球的方案数 转移:f[i][j]=f[i-1][j+1] +f[i-1][j+-1].注意: 边界f[0][1]=1; 还有当j=1或N时 #include<ios ...