LeetCode: 628. The largest product of three numbers
Just sort, because the array elements may have negative numbers, so there is a situation that needs to be considered, two smallest negative numbers * largest positive number.
AC Code
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int len = nums.length;
int a = 1, b = 1;
for(int i = len - 1; i >= 0 && i >= len - 3; i--) a *= nums[i];
for(int i = 0; i < len && i < 2; i++) b *= nums[i];
return Math.max(a, b * nums[len - 1]);
}
}