题解 | #名字的漂亮度#

名字的漂亮度

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

import java.util.*;

// 解法思路差不多,都是记录累积字符出现的次数,然后计算加权
// 下面是我21分钟的解法,然而我看到解题区有亮点的解法:是如何记录累积字符次数呢?用对应128的ASCII数组
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.nextLine();
        while (in.hasNextLine()) {
            String a = in.nextLine() ;
            List<Integer> list = new ArrayList<>();
            char[] chars = a.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (a.indexOf(String.valueOf(chars[i])) == i) {
                    int length = a.replace(String.valueOf(chars[i]), "").length();
                    int num = a.length() - length;
                    list.add(num);
                }
            }
            int max = 0;
            int n = 26;
            list.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            });
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext()) {
                Integer next = iterator.next();
                max = max + next*n;
                n--;
            }
            System.out.println(max);
        }
    }
}

全部评论

相关推荐

流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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