题解 | #名字的漂亮度#Arraylist 按频率排序

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int num=in.nextInt();

        for(int i=0;i<num;i++){

            System.out.println(beautydu(in.next()));

        }

        

    }

    //TreeSet来按照频率排序

    //漂亮度=key0*25+key1*24+...

    private static int beautydu(String s){

        ArrayList<Integerts=new ArrayList<Integer>();

        while(s.length()!=0){

            int len=s.length();

            s=s.replaceAll(String.valueOf(s.charAt(0)),"");

            ts.add(len-s.length());

        }

        ts.sort(new Comparator<Integer>(){

            public int compare(Integer a,Integer b){

                return b-a;

            }

        });

        //计算漂亮读

        int du=26;

        int result=0;

        for(int key:ts){

            result+=key*du;

            du--;

        }

        return result;

    }

}

全部评论

相关推荐

活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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