[leetcode]628. Maximum Product of Three Numbers

[leetcode]628. Maximum Product of Three Numbers


Analysis

周五啦~—— [嘻嘻~]

Given an integer array, find three numbers whose product is maximum and output the maximum product.
先把数组排序,乘积最大是最大的三个数相乘或者最小的两个数以及最大的那个数相乘(存在负数的情况)。

Implement

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

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/81036821