题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

import java.util.Scanner;

import java.util.TreeMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine().toLowerCase();

        char[] chars = s.toCharArray();

        TreeMap<Integer, Integer> map = new TreeMap<>();
        map.put(1, 0);
        map.put(2, 0);
        map.put(3, 0);
        map.put(4, 0);
        for (char c : chars) {
            if (c >= '0' && c <= '9') {
                map.put(3, map.getOrDefault(3, 0) + 1);
            } else if (c >= 'a' && c <= 'z') {
                map.put(1, map.getOrDefault(1, 0) + 1);
            } else if (c == ' ') {
                map.put(2, map.getOrDefault(2, 0) + 1);
            } else {
                map.put(4, map.getOrDefault(4, 0) + 1);
            }
        }

        for (Integer n : map.keySet()) {
            System.out.println(map.get(n));
        }
    }
}

主要是看熟不熟ASCII码吧

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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