2022-8-26 每日一题-最大的两个数-
1464. 数组中两元素的最大乘积
难度简单
给你一个整数数组 nums,请你选择数组的两个不同下标 i 和 j,使 (nums[i]-1)*(nums[j]-1) 取得最大值。
请你计算并返回该式的最大值。
1 class Solution { 2 public int maxProduct(int[] nums) { 3 int max1=0,max2=0; 4 for (int x:nums){ 5 6 if (x>max1) { 7 max2=max1; 8 max1=x; 9 } 10 else if (x>max2) max2=x; 11 12 } 13 return (max1-1)*(max2-1); 14 } 15 }
思路:找到最大和第二大值。
浙公网安备 33010602011771号