题解 | #三个数的最大乘积#
三个数的最大乘积
http://www.nowcoder.com/practice/8ae05c2913fe438b8b14f3968f64fc0b
只需要找到三个最大的和两个最小的就行啦
class Solution { public: /** * 最大乘积 * @param A int整型一维数组 * @param ALen int A数组长度 * @return long长整型 */ long long solve(int* A, int ALen) { long long minn1 = INT_MAX, minn2 = INT_MAX; long long maxn1 = INT_MIN, maxn2 = INT_MIN, maxn3 = INT_MIN; for (int i = 0; i < ALen; i++) { if (A[i] > maxn1) { maxn3 = maxn2; maxn2 = maxn1; maxn1 = A[i]; } else if (A[i] > maxn2) { maxn3 = maxn2; maxn2 = A[i]; } else if (A[i] > maxn3) { maxn3 = A[i]; } if (A[i] < minn1) { minn2 = minn1; minn1 = A[i]; } else if (A[i] < minn2) { minn2 = A[i]; } } return max((maxn1 * maxn2 * maxn3), (maxn1 * minn1 * minn2)); } };