题解 | #字符个数统计#

字符个数统计

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

其实就是去重嘛,如果用Set其实就很简单了,不过还是不想去借助已有的能力,通过其他方法来锻炼下自己,我是用StringBuilder处理的,用数组需要动态添加,这个暂时还没有想好。

public class HJ10StringStatistic {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        while (in.hasNextLine()) {            String str = in.nextLine().trim();            if (str.length() < 1 || str.length() > 500) {                System.out.println("长度范围为:1≤n≤500");                continue;            }            char[] chs = str.toCharArray();            int count = 0;            StringBuilder sb = new StringBuilder();            for (int i = 0; i < chs.length; i++) {                if ((int)chs[i] < 0 || (int)chs[i] > 127) {                    continue;                }                if (i == 0 || !sb.toString().contains("" + chs[i])) {                    sb.append("" + chs[i]);                    count++;                }            }            System.out.println(count);        }    }}
全部评论

相关推荐

10-29 15:51
嘉应学院 Java
后端转测开第一人:你把简历的学历改成北京交通大学 去海投1000份发现基本还是没面试
点赞 评论 收藏
分享
活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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