题解 | #名字的漂亮度#

名字的漂亮度

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

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            List<String> arr = new ArrayList<>();
            List<Integer> out = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                arr.add(sc.next());
            }

            arr.forEach(i -> out.add(court(i)));
            out.forEach(System.out::println);

        }
        sc.close();
    }

    private static Integer court(String i) {
        int court = 0;
        HashMap<Character, Integer> charCount = new HashMap<>();

        for (char c : i.toCharArray()) {
            if (charCount.containsKey(c)) {
                charCount.put(c, charCount.get(c) + 1);
            } else {
                charCount.put(c, 1);
            }
        }

        List<Integer> sortedCharCount = new ArrayList<>();
        charCount.forEach((key, value) -> sortedCharCount.add(value));
        //排序
        Collections.sort(sortedCharCount);
        //反转
        Collections.reverse(sortedCharCount);
        int y = 26;
        // 将每个字符的计数乘以26
        for (Integer integers : sortedCharCount) {
            court += integers * y;
            y--;
        }
        return court;
    }
}

全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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