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