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
6         """
7         nums.sort()
8         l = len(nums)
9         return max(nums[0] * nums[1] * nums[l-1], nums[l-1] * nums[l-2] * nums[l-3])
时间: 2024-08-10 02:10:59

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] 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

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 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

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 =