题解 | #连续子数组的最大乘积#
一开始使用Integer类型保存会WA,使用nextLong进行变量保存之后就可以AC了
public static void main(String[] args) { Scanner reader = new Scanner(System.in); int n = reader.nextInt(); long[] nums = new long[n]; for (int i = 0; i < n; i++) { nums[i] = reader.nextLong(); } long imax = 1; long imin = 1; long res = Long.MIN_VALUE; for (int i = 0; i < n; i++) { if (nums[i] < 0) { long t = imax; imax = imin; imin = t; } imax = Math.max(imax * nums[i], nums[i]); imin = Math.min(imin * nums[i], nums[i]); res = Math.max(res, imax); } System.out.println(res); }