[Array]628. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]
Output: 6

Example 2:

Input: [1,2,3,4]
Output: 24

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won‘t exceed the range of 32-bit signed integer.

思路:给出一个数组找出里面三个元素乘积的最大值并输出。先对整个数组进行排序,考虑到有负数的情况,因此,三个数的乘积有两种情况,2负1正,或者3个正数,如果只三个数便直接输出。

自己代码:

1 int maximumProduct(vector<int>& nums) {
2         sort(nums.begin(), nums.end());
3         int n = nums.size();
4         if(nums[0] < 0 && nums[1] < 0 && nums[0]*nums[1]*nums[n-1] > nums[n-1]*nums[n-2]*nums[n-3])
5                 return nums[0]*nums[1]*nums[n-1];
6         else
7            return nums[n-1] * nums[n-2] * nums[n-3];
8     }

优秀代码:

1 int maximumProduct(vector<int>& nums) {
2         sort(nums.begin(), nums.end());
3         int n = nums.size();
4         int m1 = nums[0]*nums[1]*nums[n-1];
5         int m2 = nums[n-1] * nums[n-2] * nums[n-3];
6         return m1 > m2?m1:m2;  //或者 return max(m1, m2);
7     }

其实不用检验前面两个元素是否是负数,因为只有两种情况,前面两个,后面一个,或者后面三个。

时间: 2024-10-29 19:11:38

[Array]628. Maximum Product of Three Numbers的相关文章

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

628. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elemen

LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

题目: Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 分析: 给定一个数组,返回其中三个元素乘积的最大值. 注意的是,这道题是可以有负数出现,且是求三个数的乘积,所以我们需要考虑负数的情况. 最先

628. Maximum Product of Three [email&#160;protected]

Given an integer array, find three numbers whose product is maximum and output the maximum product. Note: The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000]. Multiplication of any three numbers in t

[LeetCode] Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elemen

Maximum Product of Three Numbers

这道题为简单题 题目: 思路: 其实这个题主要是需要注意负数的问题,首先我把列表进行排序,可能有人到这个会有多种考虑,但是实际上返回最大值的情况只有两种,(1).列表最开始两个元素乘以最后的一个元素:(2).列表最后面3个元素相乘.根本不用考虑每个元素的正负性. 代码: 1 class Solution(object): 2 def maximumProduct(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int

Maximum Product of Word Lengths

Maximum Product of Word Lengths Total Accepted: 750 Total Submissions: 2060 Difficulty: Medium Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume tha

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product =