[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 ...
随机推荐
- Oracle事物基础
事务 1 事务定义 数据库事务是SQL语句的组合作为一个"工作单元".要么全部完成,要么全部不做. 每个事务都有一个开始和一个结束. 2 事务开始 1. 你连接到数据库并执行DML ...
- 使用 hibernate 根据映射文件生成数据库表
为了更好的显示效果,可以在hibernate.cfg.xml配置文件的<session-factory>标签里加入以下内容: 显示sql语句和格式化显示sql语句: <propert ...
- Sql Server数据库基础
--------------------------------------第一章 Sql Server数据库基础------------------------------------------ ...
- Qt5 文本编辑
[应用场景]:在编辑框中输入一段文字,用鼠标选取文字,修改工具栏上的字体.字号大小.加粗.斜体等属性,选取的文字即发生相应的变化. 一. 任何一个文本编辑器的程序都要用到QTextEdit ...
- ApiGen4.1 windows安装教程
一. ApiGen4.1版本介绍 1.ApiGen介绍 ApiGen是自动生成PHP项目的阅读文档工具. 用于从PHP源代码创建专业的API文档,类似于phpDocumentor/phpDoc. Ap ...
- liunx下NetworkManager导致网卡不能启动
前几天在客户现场,配置一台系统为redhat 6.0的服务器,这台服务器是IBM x3755,系统是预装的.在把服务器的IP地址配置完成后,使用命令不能启动网卡.提示:弹出界面 eht0:错误:激活链 ...
- LayoutInflater 与 inflate
Instantiates a layout XML file into itscorresponding View objects. LayoutInflater作用是将layout的xml布局文件实 ...
- SEO高手在扯蛋?
真正的高手SEO你在扯蛋吗?当大家都很会扯的时候,高手扯得肯定比你疼,不是他们 蛋比较敏感,而是他们的确更用力. 当你说我是SEO时,高手肯定说现在我在做的是SEM. 当你说我是SEM时,高手肯定在说 ...
- ubuntu 设置网卡为混杂模式 以及网络配置命令
1. ifconfig eth0 promisc 设置eth0为混杂模式. ifconfig eth0 -promisc 取消它的混杂模式 botnet@botnet-virtual-machine: ...
- java多线程(同步与死锁问题,生产者与消费者问题)
首先我们来看同步与死锁问题: 所谓死锁,就是A拥有banana.B拥有apple. A对B说:你把apple给我.我就把banana给你. B对A说:你把banana给我,我就把apple给你. 可是 ...