9.14百度笔试 研发A类 第二题莫名报错的问题
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int Q = in.nextInt(); int[] Ks = new int[Q]; for(int i=0;i<Q;++i){ Ks[i] = in.nextInt(); } ArrayList<Integer> poss = new ArrayList<Integer>(); for(int i=0;i<N;++i){ poss.add(i); } StringBuffer sb = new StringBuffer(); for(int i=0;i<Q;++i){ int ans = 0; ans+=poss.get(i); poss.set(i,-1); for(int j=i+1;j<=i+Ks[i];++j){ ans+=poss.get(j)-j+i+1; poss.set(j,j-i-1); } sb.append(String.valueOf(ans)).append(" "); } System.out.println(sb.substring(0,sb.length()-1).toString()); } }
算法上应该没问题,通过了四分之一用例,但是总是报错,请检查是否存在数组越界 java heap space
#百度笔试##百度##笔经#