题目:

  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. TCP同步与异步及阻塞模式,多线程+阻塞模式,非阻塞模式简单介绍

    首先我简单介绍一下同步TCP编程 与异步TCP编程. 在服务端我们通常用一个TcpListener来监听一个IP和端口.客户端来一个请求的连接,在服务端可以用同步的方式来接收,也可以用异步的方式去接收 ...

  2. http://www.highcharts.com/

    MAKE YOUR DATA COME ALIVE HIGHCHARTS CLOUD Online charts for non-techies. Create smashing, interacti ...

  3. django migration使用指南

    转自: https://docs.djangoproject.com/en/1.8/topics/migrations/

  4. Linux匿名管道与命名管道

    http://blog.chinaunix.net/uid-26000296-id-3408970.html /* * \File * main.c * \Descript * father-proc ...

  5. IDEA 14快捷键

    1.ctrl+alt+左箭头.右箭头:返回到上次浏览的代码处(相当于Eclipse的alt+左右箭头) 编辑类: Ctrl+Space 基本代码实例(类.方法.变量) Ctrl + Shift + S ...

  6. TypeError: document.formname.submit is not a function

    <form name="formname" ...> .... <input name="submit" type="submit& ...

  7. css样式,层叠顺序属性z-index

    在做项目的时候,居然单击后显示的顺序一直被别的li标签压着,最后终于找到了,是css的z-index属性赋值了,值越大,显示的层就越高 详情推荐百度百科:z-index z-index是针对网页显示中 ...

  8. Doodle Poll 投票文档

    使用Doodle Poll网页文件可以让大家投票看什么时间大家都合适.

  9. python基础——切片

    python基础——切片 取一个list或tuple的部分元素是非常常见的操作.比如,一个list如下: >>> L = ['Michael', 'Sarah', 'Tracy', ...

  10. Java中ArrayList的自我实现

    对于ArrayList相比大家都很熟悉,它是java中最常用的集合之一.下面就给出它的自我实现的java代码. 需要说明的一点是,它是基于数组创建的.所以它在内存中是顺序存储,对于查找十分的方便. p ...