zoukankan      html  css  js  c++  java
  • leetcode628

    //逻辑分析题目 + 分情况讨论 最大的数据就是若干种情况 依次找全并比较大小即可
    
    class Solution {
    public:
        int maximumProduct(vector<int>& nums) {
            sort(nums.begin() , nums.end());
            
            int len = nums.size(); //len = 3的特殊情况
            if(len == 3) return nums[0]*nums[1]*nums[2];
            
    //len >= 4情况下 两头的双因子必然其中一个 是取正的
            int maxL = nums[0] * nums[1];
            int maxR = nums[len - 1] * nums[len - 2];
            
            
            
            
            int flag = 0;
            if(maxL < maxR) {
                //Max = max(tepL , tempR);
                flag = 1;
            }
            
            
            if(flag == 0){                // - - ... 
                   if(nums[len - 1] > 0)  // - - ... +
                       return maxL * nums[len  - 1];
                   else                  //  - - ... - 该数组全负 则最大的(绝对值最小)三个数相乘
                       return maxR * nums[len - 3]; 
            }
            else{                       // + + ... + + || - - ...- + + || - + ... + + || - - ... + + +
                //if(nums[len - 3] >= 0)  
                       return max(maxL * nums[len - 1] , maxR * nums[len - 3]);
                
            }
            
           
           
            
        }
    };
    
    //[722,634,-504,-379,163,-613,-842,-578,750,951,-158,30,-238,-392,-487,-797,-157,-374,999,-5,-521,-879,-858,382,626,803,-347,903,-205,57,-342,186,-736,17,83,726,-960,343,-984,937,-758,-122,577,-595,-544,-559,903,-183,192,825,368,-674,57,-959,884,29,-681,-339,582,969,-95,-455,-275,205,-548,79,258,35,233,203,20,-936,878,-868,-458,-882,867,-664,-892,-687,322,844,-745,447,-909,-586,69,-88,88,445,-553,-666,130,-640,-918,-7,-420,-368,250,-786]
    
    //-984 -960       969 999
  • 相关阅读:
    Java动态绑定与多态
    Java中的equals,==,compareTo和compare的比较
    Java访问控制权限
    如何用eclipse进行jar文件打包?
    堆和栈的区别
    Java命名规则
    Java面向对象的基本概念
    java中length,length(),size()区别
    《剑指offer》第二十题:表示数值的字符串
    《剑指offer》第十九题:正则表达式匹配
  • 原文地址:https://www.cnblogs.com/rhythmic/p/11567675.html
Copyright © 2011-2022 走看看