LeetCode 152
Maximum Product Subarray
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.
/*************************************************************************
> File Name: LeetCode152.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: Fri 29 Apr 2016 03:46:31 PM CST
************************************************************************/ /*************************************************************************
Maximum Product Subarray 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.
************************************************************************/ #include<stdio.h> void maxAndmin( int a, int b, int c, int* max, int* min )
{
if( a > b )
{
if( a > c )
{
*max = a;
*min = b > c ? c : b;
}
else
{
*max = c;
*min = b;
}
}
else
{
if( a > c )
{
*max = b;
*min = c;
}
else
{
*max = b > c ? b : c;
*min = a;
}
}
} int maxProduct( int* nums, int numsSize )
{
int result = nums[];
int lastMax = nums[];
int lastMin = nums[];
int i;
for( i=; i<numsSize; i++ )
{
maxAndmin( nums[i], nums[i]*lastMax, nums[i]*lastMin, &lastMax, &lastMin );
result = lastMax > result ? lastMax : result;
}
printf("result is %d\n", result);
return result;
} int main()
{
int nums[] = {-, , -};
int numsSize = ;
maxProduct( nums, numsSize );
return ;
}
LeetCode 152的更多相关文章
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [算法]LeetCode 152:乘积最大子序列
题目描述: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示 ...
- [LeetCode] 152. Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- Java实现 LeetCode 152 乘积最大子序列
152. 乘积最大子序列 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] ...
- [LeetCode]152. 乘积最大子序列(DP)
题目 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示 ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- Spark Streaming 原理剖析
通过源码呈现 Spark Streaming 的底层机制. 1. 初始化与接收数据 Spark Streaming 通过分布在各个节点上的接收器,缓存接收到的流数据,并将流数 据 包 装 成 Spar ...
- WebService学习之四:关于JAX-WS 注释
基于 XML 的 Web Service 的 Java API"(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注释 ...
- UVALive 7278 Game of Cards (sg函数)
Game of Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/G Description Alice and Bob ...
- inline(内联函数)
一般来说,调用函数会造成:目前的指令位置被存储下来,程序流跳转到所调用的函数,然后执行调用函数,最后跳转回之前存储的位置.对于需要常常调用的小函数来说,这会影响程序的运行效率.所以,c99新增了inl ...
- LINUX的一些常用操作
CentOs6.7关闭防火墙(SecureCRT连接不上) 解决方法:______________________________________一.开启SSH以root用户登录Linux,打开终端, ...
- .NET Reflector插件FileDisassembler还原源码
NET Reflector,它是一个类浏览器和反编译器,可以分析程序集并向您展示它的所有秘密..NET 框架向全世界引入了可用来分析任何基于 .NET 的代码(无论它是单个类还是完整的程序集)的反射概 ...
- Thread+Handler 线程 消息循环(转载)
近来找了一些关于android线程间通信的资料,整理学习了一下,并制作了一个简单的例子. andriod提供了 Handler 和 Looper 来满足线程间的通信.例如一个子线程从网络上下载了一副图 ...
- [安卓学习]AndroidManifest.xml文件内容详解
一,重要性 AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根 ...
- ANE在ios上的使用流程和问题解决
编写ANE流程: 1. 在xcode里编写.a原生类库 2. 更改配置文件extension.xml, 定义了原生程序的扩展接口 3. 编写AS替身类,生成扩展类包.swc,解压再得到library. ...
- 初步认知MySQL metadata lock(MDL)
http://blog.itpub.net/26515977/viewspace-1208250/ 概述 随着5.5.3引入MDL,更多的Query被“Waiting for table metada ...