题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
java版本,关键在于重写排序函数的比较方式,整体难度一般
```import java.util.*;
public class Main{
public static int maxn=10001;
public static void main(String[] args){
class Sub{
int cnt;
Character po;
public void setcnt(int cnt){
this.cnt=cnt;
}
public int getcnt(){
return this.cnt;
}
public void setpo(Character po){
this.po=po;
}
public Character getpo(){
return this.po;
}
}
Scanner sa=new Scanner(System.in);
while(sa.hasNext()){
String s=sa.nextLine();
Map<Character,Sub> map=new HashMap<>();
for(int i=0;i<s.length();i++){
Sub sub=map.get(s.charAt(i));
if(null==sub){
sub=new Sub();
sub.setcnt(1);
sub.setpo(s.charAt(i));
}else{
sub.setcnt(sub.getcnt()+1);
}
map.put(s.charAt(i),sub);
}
List<Sub> list=new ArrayList<>();
for(Map.Entry<Character,Sub> entry: map.entrySet()){
list.add(entry.getValue());
}
Collections.sort(list,new Comparator<Sub>(){
@Override
public int compare(Sub sub1,Sub sub2){
if(sub1.cnt-sub2.cnt!=0)
return sub1.cnt-sub2.cnt;
else return sub2.po-sub1.po;
}
});
for(int i=list.size()-1;i>=0;i--)System.out.print(i==0?list.get(i).po+"\n":list.get(i).po);
}
}
}