---
# System prepended metadata

title: 0628. Maximum Product of Three Numbers
tags: [Leetcode, Easy]

---

# 0628. Maximum Product of Three Numbers
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/maximum-product-of-three-numbers/
## 思路 $O(N)$ $O(1)$
因为有可能出现负数，所以最大值只可能在 最大的三个数 和 最大的一个数与最小的两个数 的乘积里面出现
## Code
```java=
class Solution {
    public int maximumProduct(int[] nums) {
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        for(int num:nums){
            if(num > max1){
                max3 = max2;
                max2 = max1;
                max1 = num;
            }
            else if(num > max2){
                max3 = max2;
                max2 = num;
            }
            else if(num > max3){
                max3 = num;
            }
            
            if(num < min2){
                min1 = min2;
                min2 = num;
            }
            else if(num < min1){
                min1 = num;
            }
        }
        return Math.max(max1*max2*max3, max1*min1*min2);
    }
}
```