题解 | #牛牛的果实排序#
牛牛的果实排序
https://www.nowcoder.com/practice/975a263e2ae34a669354e0bd64db9e2a
//搜集指数
//列表排序转数组
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param trees int整型一维数组
* @return int整型一维数组
*/
public int[] primeFruits (int[] trees) {
// write code here
List<Integer> colectionList=new ArrayList<>();
for(int tree :trees){
if(isPrime(tree)){
colectionList.add(tree);
}
}
int[] arr = colectionList.stream().sorted().mapToInt(Integer::valueOf).toArray();
return arr;
}
private boolean isPrime(int x){
if(x<=1){
return false;
}
if(x==2){
return true;
}
for(int i =2;i<=x/2;i++){
if(x%i==0){
return false;
}
}
return true;
}
}
查看7道真题和解析