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

牛牛的果实排序

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

全部评论

相关推荐

代码飞升:简历差不多情况下你的学历已经加分了,海投就行,加油,不要追求尽善尽美
点赞 评论 收藏
分享
ResourceUtilization:差不多但是估计不够准确,一面没考虑到增长人口,另一方面也没考虑到能上大学的人数比例,不过我猜肯定只多不少
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务