File tree Expand file tree Collapse file tree 4 files changed +42
-4
lines changed
solution/0600-0699/0628.Maximum Product of Three Numbers Expand file tree Collapse file tree 4 files changed +42
-4
lines changed Original file line number Diff line number Diff line change 3939<!-- 这里可写当前语言的特殊实现逻辑 -->
4040
4141``` python
42-
42+ class Solution :
43+ def maximumProduct (self , nums : List[int ]) -> int :
44+ n = len (nums)
45+ nums.sort()
46+ # 全负 0 1 n-1
47+ # 全正 n-1 n-2 n-3
48+ # 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
49+ return max (nums[0 ] * nums[1 ] * nums[n - 1 ], nums[n - 1 ] * nums[n - 2 ] * nums[n - 3 ])
4350```
4451
4552### ** Java**
4653
4754<!-- 这里可写当前语言的特殊实现逻辑 -->
4855
4956``` java
50-
57+ class Solution {
58+ public int maximumProduct (int [] nums ) {
59+ Arrays . sort(nums);
60+ int n = nums. length;
61+ // 全负 0 1 n-1
62+ // 全正 n-1 n-2 n-3
63+ // 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
64+ return Math . max(nums[0 ] * nums[1 ] * nums[n - 1 ], nums[n - 1 ] * nums[n - 2 ] * nums[n - 3 ]);
65+ }
66+ }
5167```
5268
5369### ** ...**
Original file line number Diff line number Diff line change 4646### ** Python3**
4747
4848``` python
49-
49+ class Solution :
50+ def maximumProduct (self , nums : List[int ]) -> int :
51+ n = len (nums)
52+ nums.sort()
53+ return max (nums[0 ] * nums[1 ] * nums[n - 1 ], nums[n - 1 ] * nums[n - 2 ] * nums[n - 3 ])
5054```
5155
5256### ** Java**
5357
5458``` java
55-
59+ class Solution {
60+ public int maximumProduct (int [] nums ) {
61+ Arrays . sort(nums);
62+ int n = nums. length;
63+ return Math . max(nums[0 ] * nums[1 ] * nums[n - 1 ], nums[n - 1 ] * nums[n - 2 ] * nums[n - 3 ]);
64+ }
65+ }
5666```
5767
5868### ** ...**
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int maximumProduct (int [] nums ) {
3+ Arrays .sort (nums );
4+ int n = nums .length ;
5+ return Math .max (nums [0 ] * nums [1 ] * nums [n - 1 ], nums [n - 1 ] * nums [n - 2 ] * nums [n - 3 ]);
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maximumProduct (self , nums : List [int ]) -> int :
3+ n = len (nums )
4+ nums .sort ()
5+ return max (nums [0 ] * nums [1 ] * nums [n - 1 ], nums [n - 1 ] * nums [n - 2 ] * nums [n - 3 ])
You can’t perform that action at this time.
0 commit comments