题解 | #牛牛的果实排序#

牛牛的果实排序

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;
    }
}

全部评论

相关推荐

牛客221235529号:土木只用写:土木 男 能吃苦 就好了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务