题解 | #记负均正#

字符统计

http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

import java.util.*;
public class Main {
    public static void main(String[] args){

        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            Map<Character, Integer> map = new HashMap<>();
            String str = in.nextLine();
            List<Character> list = new ArrayList<>();
            for(char c:str.toCharArray()){
                if(map.get(c) == null){
                    map.put(c, 1);
                    list.add(c);
                } else {
                    map.put(c, map.get(c)+1);
                }
            }
            Collections.sort(list, new Comparator<Character>(){
                @Override
                public int compare(Character c1, Character c2) {
                    return map.get(c1)!=map.get(c2)?
                            map.get(c2)-map.get(c1):
                            c1-c2;
                }
            });
            for(char c:list){
                System.out.print(c);
            }
            System.out.println();
        }
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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