# C#LeetCode刷题之#628-三个数的最大乘积（ Maximum Product of Three Numbers）

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Input: [1,2,3]

Output: 6

Input: [1,2,3,4]

Output: 24

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 the input won’t exceed the range of 32-bit signed integer.

```public class Program {

public static void Main(string[] args) {
int[] nums = null;

nums = new int[] { -4, 12, 9, -20 };
var res = MaximumProduct(nums);
Console.WriteLine(res);

}

private static int MaximumProduct(int[] nums) {
//先排序
Array.Sort(nums);
//记录数组长度
int length = nums.Length;
//该题主要考查数组中包含负数的情况
//如果全为正数，排序后输出后3位的乘积即可
//否则，最大值一定在最小的2个负数和最大数的乘积、后3位值的乘积中产生
//直接比较它们的大小输出即可
//先存下数组后3位值的乘积
int product = nums[length - 1] * nums[length - 2] * nums[length - 3];
//取前2位和最后1位的乘积、后3位值的乘积中的最大值
product = Math.Max(product, nums[length - 1] * nums[1] * nums[0]);
//输出这个最大值
return product;
}

}```

`960`