zoukankan      html  css  js  c++  java
  • leetcode 152. Maximum Product Subarray --------- java

    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.

    找出最大的相乘的数字,很简单,代码还可以优化的地方很多。但是速度还可以。

    public class Solution {
        public int maxProduct(int[] nums) {
            int len = nums.length;
            if( nums.length == 1)
                return nums[0];
            int[] result = new int[2];
            int target = 0;
            
            int left_right = 0,right_left = 0;
            for( int i = 0 ; i < len ; i ++){
                if( nums[i] == 0){
                    target = Math.max(target,result[0]);
                    result[0] = 0;
                    result[1] = 0;
                    target = 0;
                }else if( nums[i] > 0 ){
                    if( result[0] != 0)
                        result[0] *= nums[i];
                    else
                        result[0] = nums[i];
                }else if( nums[i] < 0 ){
                    if( result[1] == 0 ){
                        result[1] = nums[i]*(result[0] == 0?1:result[0]);
                        result[0] = 0;
                    }
                    else{
                        if( result[0] == 0){
                            result[0] = result[1]*nums[i];
                            result[1] = 0;
                        }else{
                            result[0] = result[0]*result[1]*nums[i];
                            result[1] = 0;
                        }
                    }
                }
                left_right = Math.max(Math.max(result[0],target),left_right);
    
            }
    
            target = 0;
            result[0] = 0;
            result[1] = 0;
            for( int i = len-1;i>=0;i--){
                if( nums[i] == 0){
                    target = Math.max(target,result[0]);
                    result[0] = 0;
                    result[1] = 0;
                    target = 0;
                }else if( nums[i] > 0 ){
                    if( result[0] != 0)
                        result[0] *= nums[i];
                    else
                        result[0] = nums[i];
                }else if( nums[i] < 0 ){
                    if( result[1] == 0 ){
                        result[1] = nums[i]*(result[0] == 0?1:result[0]);
                        result[0] = 0;
                    }
                    else{
                        if( result[0] == 0){
                            result[0] = result[1]*nums[i];
                            result[1] = 0;
                        }else{
                            result[0] = result[0]*result[1]*nums[i];
                            result[1] = 0;
                        }
                    }
                }
            right_left = Math.max(Math.max(result[0],target),right_left);
            }
    
    
            return Math.max(left_right,right_left);
        }
    }
  • 相关阅读:
    Thrift安装编译指南
    Linux磁盘与文件系统管理
    你懂得C,所以C++也不在话下
    加速NFV(网络功能虚拟化)数据面:SR-IOV和DPDK[原文意译]
    十张图看懂SDN与NFV的区别与联系?
    Lambda表达式用法
    论文写作+gnuplot制图
    私钥、公钥、数字签名和数字证书
    synchronized和Lock的异同
    介绍HTTP协议的传输过程
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6079446.html
Copyright © 2011-2022 走看看