题目:

  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.

题意:

  题目中给出一个(至少包含一个元素)整形数组,求一个子数组(元素连续),使得其元素之积最大。

  最直接了当的方法,当然是暴力穷举,但是其O(n^2)是无法通过LeetCode的OJ的。

  以下给出了本题的解决方法,O(n)时间复杂度,C++实现。

 class Solution {
public:
int maxProduct(int A[], int n) {
int nMaxPro = A[];
int max_tmp = A[];
int min_tmp = A[]; for(int i = ; i< n; ++i){
int a = A[i] * max_tmp;
int b = A[i] * min_tmp; max_tmp = (( a > b ? a : b ) > A[i]) ? ( a > b ? a : b ) : A[i];
min_tmp = (( a < b ? a : b ) < A[i]) ? ( a < b ? a : b ) : A[i]; nMaxPro = (nMaxPro > max_tmp) ? nMaxPro : max_tmp;
} return nMaxPro;
}
};

运行结果:

希望各位看官不吝赐教,小弟感谢~!

[leetCode][001] Maximum Product Subarray的更多相关文章

  1. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  2. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  3. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  4. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. Java for LeetCode 152 Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. leetcode 152. Maximum Product Subarray --------- java

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. C#解leetcode 152. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. LeetCode之Maximum Product Subarray

    1.(原文)问题描述 Find the contiguous subarray within an array (containing at least one number) which has t ...

  9. [leetcode]152. Maximum Product Subarray最大乘积子数组

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

随机推荐

  1. DrawText

    该函数在指定的矩形里写入格式化的正文,根据指定的方法对正文格式化(扩展的制表符,字符对齐.折行等).   int DrawText(HDC hDC, // 设备描述表句柄 LPCTSTR lpStri ...

  2. 友盟消息推送和更新XML配置

    <receiver android:name="com.umeng.message.NotificationProxyBroadcastReceiver" android:e ...

  3. 破解php-screw加密过的文件有效方法

    今天终于搞定更改过密钥的php-screw解密问题,乐呵一下! 改进下 这样就可以解密任何加密过的PHP源码(包括更改过密钥的),解密的原理稍后具体列出,先说下如何加密 列出之前写使用php scre ...

  4. virsh常用命令

    必须启动libvirtd,才能用virsh查看kvm后台. # systemctl start libvirtd 查看网络 # virsh net-list 启动default网络 # virsh n ...

  5. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  6. 【转】SQL中内连接和外连接

    如表     -------------------------------------------------     table1 | table2 |     ----------------- ...

  7. sc 与net命令的区别

    windows服务操作命令有sc和net 两个命令; sc stop serviceName  sc start serviceName net stop serviceName  net start ...

  8. 解决虚拟机 正在决定eht0 的ip信息失败 无链接-- 添加虚拟网卡

    添加步骤:1.进入设备管理器 2.点下一步3.继续下一步 4.继续往下走

  9. After Effects的4种抠像插件比较分析

    前景 背景 1.keylight(1.2) 2.Primatee Keyer Pro4.0 3.Zbig [边界生硬] 4.Power Matte v2 [速度很慢,边界生硬]

  10. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...