题目 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 思路 数据长度不大,选择排序。因为可能存在负数的情况,最后返回的时候判断一下最大数值。 代码 class Solution { public: int maximumProduct(vector<int>& nums) { sort(nums.begin(), nums.end()); int n = nums.size(); return max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]); } };