LeetCode Maximum Product Subarray 最大子序列积
题意:给一个size大于0的序列,求最大的连续子序列之积。(有正数,负数,0)
思路:正确分析这三种数。0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样就没有0了。
接着是负数,负数如果遇到一个负数,可能反而比那个正数要大,所以正负数都要保存,遍历一次即可。在奇数个负数时,其实可能的只有2种:(1)包含最前面一个负数的序列(2)包含最后面一个负数的序列(当然不包含最前面1个,同理(1)也是)。
class Solution {
public:
int maxProduct(vector<int>& nums) {
int a=, b=, res=nums[];
for(int i=; i<nums.size(); i++)
{
int t=a;
a=max(b*nums[i],a*nums[i]); //a保存大的
b=min(t*nums[i],b*nums[i]); //b保存小的
res=max(res, a);
//if(a<=0) a=1; //必须保证a大于0
if(b>=) b=; //必须保证b小于0。为了不改变原值,只能为1。
}
return res;
}
};
AC代码
LeetCode Maximum Product Subarray 最大子序列积的更多相关文章
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Product Subarray 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [leetcode]Maximum Product Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...
随机推荐
- Codeforces Round #285 (Div. 1) A. Misha and Forest 拓扑排序
题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 L ...
- MVC3 ModelBinder
1.Model Binder从哪些地方获取数据(找到数据后会停止继续寻找) MVC 框架内置默认的 Model Binder 是 DefaultModelBinder 类.当 Action Invok ...
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 【BZOJ】【1927】【SDOI2010】星际竞速
网络流/费用流 比较简单的一题,对于每个星球,将它拆成两个点,然后二分图建模:左部结点与S相连,流量为1费用为0:右部结点与T相连,流量为1费用为0:对于每条航道x->y,连边x->y+n ...
- ural 1864
题意描述不清 而且还卡精度 ~~ #include <cstdio> #include <cstring> #include <iostream> using ...
- crud springmvc
实体类:Student.java package demo.entity; public class Student { private int id; private String name; pr ...
- firefox无法安装未通过验证的扩展
firefox43版本无法安装未验证附加组件,利用以下方法: 1.进入firefox about:config页面中 2.搜索xpinstall.signatures.required,将值改为fa ...
- ZOJ 2724 Windows Message Queue (优先级队列,水题,自己动手写了个最小堆)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- IOS委托设计模式(摘自IOS开发指南)
- POJ3176Cow Bowling
http://poj.org/problem?id=3176 题意:就是一个数塔的问题,属于最简单的动态规划题了吧,数塔从上到下只能找它下面的和它下面的右边的那一个想加,加到最后一行,看加哪个数可以保 ...