滴滴算法题疑问?
import java.util.Scanner;
import java.util.StringTokenizer;
//1 2 3 4 -1 -2 -4 -3 1 2
publicclass Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
StringTokenizer st = new StringTokenizer(line, " ");
int[] list = new int[st.countTokens()];
for (int i = 0; i < list.length; i++) {
list[i] = Integer.parseInt(st.nextToken());
}
int maxLength = 0;
for (int i = list.length - 1; i >= 0; i--) {
int sum = list[0];
boolean needbreak = false;
for (int j = 1; j <= i; j++) {
sum += list[j];
}
if (sum == 0) {
maxLength = i + 1;
needbreak = true;
break;
}
if (needbreak) {
break;
}
}
for (int k = 0; k < maxLength; k++) {
System.out.print(list[k] + " ");
}
}
}