public int solution(int[] A) { // write your code in Java SE 8 int n = A.length; long[] prefix = new long[n]; long[] suffix = new long[n]; prefix[0] = A[0]; for(int i = 1; i < n; i++) { prefix[i] = prefix[i-1] + A[i]; } suffix[n-1] = A[n-1]; for(int i = n-2; i >= 0; i--) { suffix[i] = suffix[i+1] + A[i]; } long res = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { res = Math.min(res, Math.abs(prefix[i-1] - suffix[i])); } return (int)res; }
没有评论:
发表评论