Maximum Product of Two Elements in an Array
给一个数组, 都是正数, 求其中两个数最大的乘积. 就是找到最大和第二大的数. 直接用两个变量表示就可以了.
class Solution {
public int maxProduct(int[] nums) {
int m = Integer.MIN_VALUE;
int mm = Integer.MIN_VALUE;
int n = nums.length;
for(int i = 0; i < n; i++) {
if(nums[i] > m) {
mm = m;
m = nums[i];
} else if(nums[i] > mm){
mm = nums[i];
}
}
return (m - 1) * (mm - 1);
}
}