fisrt sort the array,cmp the num=nums[n-1]*nums[n-2]*nums[n-3] and the num=nums[0]*nums[1]*nums[n-1] return the maxer


  • -1
    M

    class Solution {
    public:
    int maximumProduct(vector<int>& nums) {
    sort(nums.begin(),nums.end());
    int n=nums.size();
    if(nums[0]>=0||nums[n-1]<=0) return nums[n-1]*nums[n-2]*nums[n-3];
    int ma=nums[0]*nums[1];
    ma=max(ma,nums[n-2]nums[n-3]);
    return ma
    nums[n-1];
    }
    };


  • 1
    2

    "If you want to include code in your post,\r\nplease surround your code block with 3 backticks ```.

    For example:

    def helloWorld():   
        pass
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.