Maximum Product Subarray 最大连续乘积子集
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
此题要求是要在一个无需的数组找出一个乘积最大的连续子数组
例如[2,3,-2,4],因为可能有负数,可以设置两个临时变量mint和maxt,mint存储遇到负数之后相乘变小的乘积,maxt用来存储大的乘积。
比如2*3=6,此时,mint = maxt = 6,当下次遇到-2时,mint = maxt = -12,此时乘积-12小于-2,则应使maxt = -2。为避免下个数还是负数,应使mint不变,若下次遇到负数,则乘积比maxt大,然后交换……
具体看代码:
public class Solution {
public int maxProduct(int[] A) {
int n = A.length;
int mint = 1;
int maxt = 1; int maxvalue = A[0];
for(int i = 0 ; i < n ; i++){
if(A[i] == 0){
mint = 1;
maxt = 1;
if(maxvalue < 0)
maxvalue = 0;
}else{
int curmax = maxt * A[i];
int curmin = mint * A[i]; maxt = curmax > curmin ? curmax : curmin;
mint = curmax > curmin ? curmin : curmax; if(maxt < A[i])
maxt = A[i]; if(mint > A[i])
mint = A[i]; if(maxt > maxvalue)
maxvalue = maxt;
}
} return maxvalue; }
}
Maximum Product Subarray 最大连续乘积子集的更多相关文章
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列
题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题 法一:直接暴力求解 时 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 46.Maximum Product Subarray(最大乘积子数组)
Level: Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- python学习,day3:函数式编程,局部变量和全局变量
# coding=utf-8 # Author: RyAn Bi school = 'THU' #全局变量 def change_name(name): global age #在函数中,用globa ...
- python实现数据库增删改查
column_dic = {"id": 0, "name": 1, "age": 2, "phone": 3, &quo ...
- Apache Maven的入门使用之项目的基本构建(1)
前言 最近在研究java框架struts2的相关漏洞,然后就去看了官方给出的文档.在看文档的过程中发现使用到了Apache Maven这个项目管理工具,我在网上搜索了一下,大多数文章都写得不是很系统, ...
- HttpURLConnection发送GET、POST请求
HttpURLConnection发送GET.POST请求 /** * GET请求 * * @param requestUrl 请求地址 * @return */ public String get( ...
- This operation is not available unless admin mode is enabled: FLUSHDB
报错: This operation is not available unless admin mode is enabled: FLUSHDB 参考内容: https://www.cnblogs ...
- Linux 操作系统常用的三种流012
Linux 操作系统常用的三种流: 0 标准输入流 1 标准输出流 2 标准错误流 通常在写脚本启动程序,写log时候,会出现如下写法: nohup commod > log.txt 2> ...
- python-TCP模拟ftp文件传输
#!/usr/bin/python #coding=utf-8 #server from socket import* import sys,os def command(): l=[ "W ...
- 选择适用才最好 盘点MySQL备份方式
我们要备份什么? 一般情况下, 我们需要备份的数据分为以下几种 数据 二进制日志, InnoDB事务日志 代码(存储过程.存储函数.触发器.事件调度器) 服务器配置文件 备份工具 这里我们列举出常用的 ...
- 浅谈FileReader
FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据. 了解https://develope ...
- 工作中遇到的一道SQL应用题
登录日志表 CREATE TABLE [dbo].[LoginLog]([Seq] [int] NOT NULL IDENTITY(1, 1), --Seq[UserId] [varchar] (2 ...