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

牛牛的果实排序

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

全部评论

相关推荐

06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务