全部评论
https://tans.fun/archives/tecent-2022-exam 写了一篇题解,欢迎评论😀
第二题只能过70多,还能咋优化兄弟们 static Set<Integer> set=new HashSet<>(); public static int getNumber(int[] a){ List<Integer> list=new ArrayList<>(); List<Integer> help=new ArrayList<>(); for(int i=0;i<a.length;i++){ list.add(a[i]); help.add(a[i]); if(!isPrime(i+1)){ set.add(i+1); } } while(true){ int count=0; int n=list.size(); for(int i=0;i<n;i++){ if(set.contains(i+1)){ help.remove(i-count); count++; } } list=help; if(list.size()==1) break; } return list.get(0); } public static boolean isPrime(int n){ if(n==1) return false; for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ return false; } } return true; }
相关推荐
点赞 评论 收藏
分享

点赞 评论 收藏
分享