题解 | #字符统计#

字符统计

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] array = s.toCharArray();
	  	//对应ascii表的128个字符
        int[] a = new int[128];
	  	//将字符对应ascii表的值作为下标统计其个数
        for (char c : array) {
            a[(int) c]++;
        }
	  	//单个字符出现的次数不会超过输入字符串的长度
        int max = s.length();
        while (max > 0){
		  	//从0开始按照ascii表的顺序遍历
            for (int i = 0; i < a.length; i++) {
                if (a[i] == max){
                    System.out.print((char) i);
                }
            }
            max--;
        }
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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