题解 | #矩阵乘法计算量估算#

字符统计

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()) {
        String str = in.nextLine();
        System.out.println(solution(str));
    }
}

public static String solution(String string){
    StringBuilder result = new StringBuilder("");
    ArrayList<Twos> twos = new ArrayList<>();
    while(string.length() > 0){
        char c = string.charAt(0);
        String str = string.replaceAll(c+"","");
        int length = string.length() - str.length();
        string = str;
        Twos twos1 = new Twos(c,length);
        twos.add(twos1);
    }
    twos.sort(new Comparator<Twos>() {
        @Override
        public int compare(Twos twos, Twos t1) {
           if(twos.b == t1.b){
                return twos.a - t1.a;
            }else{
                return t1.b - twos.b;
            }
        }
    });
    for(int i = 0;i < twos.size();i++){
        result.append((char)(twos.get(i).a));
    }
    return result.toString();
}

public static class Twos{
    int a;
    int b;
    public Twos(char a,int  b){
        this.a = a;
        this.b = b;
    }
}

}

全部评论

相关推荐

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

创作者周榜

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