题解 | #名字的漂亮度#

名字的漂亮度

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
榕城小榕树:你是我见过最幸福的牛客男孩
点赞 评论 收藏
分享
nus2201602...:兄弟,你这个简历撕了丢了吧,就是一坨,去找几个项目,理解项目流程,看几遍就是你的了,看看八股就去干了,多看看牛客里别人发出来的简历,对着写,你这写的啥啊,纯一坨
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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