LeetCode: Maximum Product Subarray
很久没练只能看别人代码了
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相关推荐
- LeetCode Maximum Product Subarray(最大子数组乘积)
Find the contiguous subarray within an array (containing at least one number) which has the large ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- 【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
152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode Maximum Product of Word Lengths(位操作)
问题:给出一个字符串数组,要求求出两个没有共同字符的字符串的最大积 思路:第一种方法是直接枚举任意两个字符串,看是否有公共字符,如果没有,则计算乘积,并更新最大值. 第二种方法,因为字符范围是a-z, ...
- Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray | 152. 乘积最大子数组(Java)
题目 https://leetcode.com/problems/maximum-product-subarray/ 题解 class Solution {public int maxProduct( ...
- 子数组的最大乘积 Maximum Product Subarray
为什么80%的码农都做不了架构师?>>> 问题: Find the contiguous subarray within an array (containing at lea ...
最新文章
- spring三: 装配bean( 在xml中进行显式配置, 在java中进行显式配置)
- 7篇必读ACM MM 2019论文:图神经网络+多媒体
- 字符串(strcmp)
- nrf51822-配对绑定实现过程
- 如何用php采集照片,使用PHP采集远程图片
- 【HibernateHibernate实体关系映射——双边的多对一、一对多关系
- python画tan_Python入门之三角函数tan()函数实例详解
- 大型网站技术架构的原理与分析
- 向量自回归VAR模型、结构向量自回归SVAR模型、VMA模型
- gitlab 账号注册及修改资料
- VBA: 字典的介绍与实例
- 【矩阵论】矩阵基本概念 + 矩阵广义逆
- ubuntu安装以太方mist
- 业务口堆叠和堆叠子卡_通过堆叠最酷的库来构建和部署简单的ml工具
- 明基发布T系列智能商务投影机
- 快速学会使用association和collection
- 太阳计算机音乐,邱振哲《太阳》[FLAC/MP3-320K]
- 【MATLAB】线性规划问题中碰到绝对值的情况
- Xcode11没有iOS14的真机包,Xcode12没有iOS8的真机包,用到的时候网上都要积分收费,免费的不好找,因此只有自己保存一份使用到的时候才不慌
- CGB2104-Day20
热门文章
- 展开收起功能实现_Windows10自带的7大隐藏功能,个个都实用!
- We Are Cisco|25年后,为什么我仍是思科认证互联网专家
- 使用 Spot 低成本运行 Job 任务
- 6 个 K8s 日志系统建设中的典型问题,你遇到过几个?
- macos 全局快捷键 打开 iterm_在 macOS 上实用的十大软件!你get了吗?
- KDD_cup99 pytorch
- java 服务器所在盘符_JAVA获取文件本身所在的磁盘位置 | 学步园
- red hat linux 7.1 使用手册!,Red Hat Linux 7.1使用手册(中)
- 计算机系统基础期中考试,计算机系统基础第二次作业.docx
- python编程软件开发_Python编程对软件开发人员很重要?