[LeetCode]题解(python):152-Maximum Product Subarray
题目来源:
https://leetcode.com/problems/maximum-product-subarray/
题意分析:
给定一个数组,这个数组所有子数组都有一个乘积,那么返回最大的乘积。
题目思路:
由于题目输入的都是整型的,所以所有的数相差获得的是最大或者最小值。那么我们同时记录可以获得的最大和最小值,每次更新。
代码(python):
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
res = nums[0]
ans = max_ans = min_ans = nums[0]
i = 1
while i < len(nums):
tmp1,tmp2 = nums[i]*max_ans,nums[i]*min_ans
max_ans = max(nums[i],max(tmp1,tmp2))
min_ans = min(nums[i],min(tmp1,tmp2))
ans = max(max_ans,ans)
i += 1
return ans
[LeetCode]题解(python):152-Maximum Product Subarray的更多相关文章
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- Collections类方法详解
Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序(Sort)使用sort方法可以根据元素的自然顺序 对 ...
- [Linked List]Reverse Nodes in k-Group
Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...
- 解决HTML5中placeholder属性兼容性的JQuery插件
//调用方法 $(function () { $(".pHolder").jason(); }); //HTML代码 <input type="text&quo ...
- HTML 5 学习之应用程序缓存
什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏 ...
- 《小猪CMS(PigCms)多用户微信营销服务平台系统V6.1完美破解至尊版带微用户管理CRM+微信支付》
<小猪CMS(PigCms)多用户微信营销服务平台系统V6.1完美破解至尊版带微用户管理CRM+微信支付> 之前发布了不少微赢的多用户微信网站源码,今天为我的小伙伴们准备的是功能非常强悍, ...
- 小米4c刷机
[测评] 试用小米4c最新波兰开发版 http://www.miui.com/thread-3048163-1-1.html [教程] 小米4C刷第三方recovery 以及ROOT教程 http:/ ...
- skynet的流程2
http://blog.csdn.net/xiarendeniao/article/details/38613161 http://www.jiandan.ren/2015/06/skynet-hel ...
- eclipse android sdk javadoc
sdk 的函数不提示帮助信息 查了下是现在adt版本没有doc文件夹,拷贝了早期的版本docs过来 其他具体操作如下:http://blog.csdn.net/lyh7736362/article/d ...
- windows、linux创建子进程
在windows下创建子进程较常用到的API就是CreateProcess,可以通过以下的方式启动一个新进程: STARTUPINFO si = {0}; PROCES ...
- CentOS Linux 语言环境设置
程序运行使用一套语言需要有字符集(数据)和字体(显示),Locale是根据计算机用户所使用的语言,所在国家或者地区,以及当地的文化传统所定义的一个软件运行时的语言环境. 一.locale详解 在 Li ...