题解 | #名字的漂亮度#

名字的漂亮度

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

import java.util.*;
import java.util.stream.Stream;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String n = in.nextLine();
        while (in.hasNextLine()) {
            String s = in.nextLine();
            char[] cs = s.toCharArray();
            List<Node> nodes = new ArrayList<>();
            nodes.add(new Node(s.charAt(0), 1));
            for (int i = 1; i < s.length(); i++) {
                boolean has = false;
                for (int j = 0; j < nodes.size(); j++) {
                    if (s.charAt(i) == nodes.get(j).c) {
                        nodes.get(j).count++;
                        has = true;
                    }
                }
                if (!has) {
                    char c = s.charAt(i);
                    Node node = new Node(c, 1);
                    nodes.add(node);
                }
            }
            Collections.sort(nodes);
            int sum = 0;
            int j = 26;
            for (int i = 0; i < nodes.size(); i++) {
                sum += j * nodes.get(i).count;
                j--;
            }
            System.out.println(sum);
        }

    }
}
class Node implements Comparable {
    char c;
    int count;

    Node (char c, int count) {
        this.c = c;
        this.count = count;
    }

    @Override
    public int compareTo(Object o) {
        Node node = (Node)o;
        return node.count - this.count;
    }
}

全部评论

相关推荐

凌小云:问题太大了,首先把教育背景放前面。不然简历不用看就看被pass了。然后两个项目写了和没写一样,不如商城+点评的描述。那专业技能,前面来个技术名,后面一点都不见具体那些了。你说你熟练java,说说java反射实现方式,那些地方用,io都有那些。这让面试官怎么问。这份简历看下来,没一点问的希望。看着技术栈用的多,亮点也没解决什么实际问题。很差的一份简历,患上技术堆砌的毛病了
我的简历长这样
点赞 评论 收藏
分享
10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
求个付费实习岗位:这种就是吃满时代红利又没啥技术水平,只能靠压力学生彰显优越感的老登,别太在意了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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