题解 | #字符个数统计#

字符个数统计

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

题目 字符个数统计

日期 2022/03/17

思路

用HashSet存储,只存入ASCII码在0~127之间的字符

代码

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println(countChar(str));
    }
    public static int countChar(String str){
        Set<Character> set = new HashSet<>();
        int len = str.length();
        for(int i=0;i<len;i++){
            char ch = str.charAt(i);
            if(ch>=0 && ch<=127)
                set.add(ch);
        }
        return set.size();
    }
}
全部评论

相关推荐

06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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