双指针,向中间收敛
当[0,l]和[r,len-1]时,更新奖金数,每次循环l++,判断右区间大小,更新r指针
双指针经典写法while(l<=r){l++;r--;},l,r分别初始化为-1和数组长度。
本题如果用int类型,AC89.6%,一般这种首先把int全改成long尝试一下,然后再检查边界。PS:java 4种整数1,2,4,8字节。
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] input = new long[n]; for(int i = 0; i < n; i++){ input[i] = sc.nextInt(); } System.out.println("debug"); int l = -1, r = input.length; long cnt = 0, left = 0, right = 0; while (l <= r) { l++; left += input[l]; while (right < left) { r--; right += input[r]; } if(right == left && l <= r){ cnt = left; } } System.out.println(cnt); } }