统计字符----给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符

  • 首先,把字符串转为字符数组
  • 定义一个哈希数组,存储遍历过的字符,当遇到首次出现3次的字符,则返回
public  class Test{
    public static void main(String[] args) {
        String str = "Have you ever gone shopping";
        System.out.println(firstThree(str));
    }
    public static char firstThree(String str) {
                                                      //str变成char[]
        char[] data = str.toCharArray();
        int[] hash = new int[256];
        for (int i = 0; i < data.length; i++) {
                                                         //hash
            char c = data[i];
            if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
                hash[c]++;
                if (hash[c] == 3) {
                    return c;
                }
            }

        }
        return '0';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值