很久没练只能看别人代码了

 1 class Solution {
 2 public:
 3     int maxProduct(int A[], int n) {
 4         if (n == 0) return 0;
 5         int curMax, curMin, ans;
 6         ans = curMax = curMin = A[0];
 7         for (int i = 1; i < n; ++i) {
 8             int tmp = curMin*A[i];
 9             curMin = min(A[i], min(tmp, curMax*A[i]));
10             curMax = max(A[i], max(tmp, curMax*A[i]));
11             ans = max(ans, curMax);
12         }
13         return ans;
14     }
15 };

C#

 1 public class Solution {
 2     public int MaxProduct(int[] nums) {
 3         if (nums.Length == 0) return 0;
 4         int curMax = nums[0], curMin = nums[0], ans = nums[0];
 5         for (int i = 1; i < nums.Length; i++) {
 6             int tmp = curMin * nums[i];
 7             curMin = Math.Min(nums[i], Math.Min(tmp, curMax * nums[i]));
 8             curMax = Math.Max(nums[i], Math.Max(tmp, curMax * nums[i]));
 9             ans = Math.Max(ans, curMax);
10         }
11         return ans;
12     }
13 }

View Code

转载于:https://www.cnblogs.com/yingzhongwen/p/4012148.html

LeetCode: Maximum Product Subarray相关推荐

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

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

  2. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

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

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

  4. LeetCode 152. Maximum Product Subarray

    152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...

  5. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  6. LeetCode Maximum Product of Word Lengths(位操作)

    问题:给出一个字符串数组,要求求出两个没有共同字符的字符串的最大积 思路:第一种方法是直接枚举任意两个字符串,看是否有公共字符,如果没有,则计算乘积,并更新最大值. 第二种方法,因为字符范围是a-z, ...

  7. Maximum Product Subarray

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

  8. leetcode 152. Maximum Product Subarray | 152. 乘积最大子数组(Java)

    题目 https://leetcode.com/problems/maximum-product-subarray/ 题解 class Solution {public int maxProduct( ...

  9. 子数组的最大乘积 Maximum Product Subarray

    为什么80%的码农都做不了架构师?>>>    问题: Find the contiguous subarray within an array (containing at lea ...

最新文章

  1. spring三: 装配bean( 在xml中进行显式配置, 在java中进行显式配置)
  2. 7篇必读ACM MM 2019论文:图神经网络+多媒体
  3. 字符串(strcmp)
  4. nrf51822-配对绑定实现过程
  5. 如何用php采集照片,使用PHP采集远程图片
  6. 【HibernateHibernate实体关系映射——双边的多对一、一对多关系
  7. python画tan_Python入门之三角函数tan()函数实例详解
  8. 大型网站技术架构的原理与分析
  9. 向量自回归VAR模型、结构向量自回归SVAR模型、VMA模型
  10. gitlab 账号注册及修改资料
  11. VBA: 字典的介绍与实例
  12. 【矩阵论】矩阵基本概念 + 矩阵广义逆
  13. ubuntu安装以太方mist
  14. 业务口堆叠和堆叠子卡_通过堆叠最酷的库来构建和部署简单的ml工具
  15. 明基发布T系列智能商务投影机
  16. 快速学会使用association和collection
  17. 太阳计算机音乐,邱振哲《太阳》[FLAC/MP3-320K]
  18. 【MATLAB】线性规划问题中碰到绝对值的情况
  19. Xcode11没有iOS14的真机包,Xcode12没有iOS8的真机包,用到的时候网上都要积分收费,免费的不好找,因此只有自己保存一份使用到的时候才不慌
  20. CGB2104-Day20

热门文章

  1. 展开收起功能实现_Windows10自带的7大隐藏功能,个个都实用!
  2. We Are Cisco|25年后,为什么我仍是思科认证互联网专家
  3. 使用 Spot 低成本运行 Job 任务
  4. 6 个 K8s 日志系统建设中的典型问题,你遇到过几个?
  5. macos 全局快捷键 打开 iterm_在 macOS 上实用的十大软件!你get了吗?
  6. KDD_cup99 pytorch
  7. java 服务器所在盘符_JAVA获取文件本身所在的磁盘位置 | 学步园
  8. red hat linux 7.1 使用手册!,Red Hat Linux 7.1使用手册(中)
  9. 计算机系统基础期中考试,计算机系统基础第二次作业.docx
  10. python编程软件开发_Python编程对软件开发人员很重要?