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.

找出3个数,使他们的乘积最大,数字会有负数

C++(79ms):

 1 class Solution {
 2 public:
 3     int maximumProduct(vector<int>& nums) {
 4         sort(nums.begin(),nums.end()) ;
 5         int a = nums[0] * nums[1] * nums[nums.size()-1] ;
 6         int b = nums[nums.size()-3] * nums[nums.size()-2] * nums[nums.size()-1] ;
 7         if (a >= b)
 8             return a ;
 9         else
10             return b ;
11     }
12 };
时间: 2024-11-08 00:56:36

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]

[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: 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 =