JZ34-第一个只出现一次的字符位置

第一个只出现一次的字符

https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&tab=answerKey

class Solution2 {
    public int FirstNotRepeatingChar(String str) {
        if (str == null || str.length() == 0) {
            return -1;
        }
        int[] temp = new int[256];
        for (int i = 0; i < str.length(); i++) {
            temp[str.charAt(i)]++; //把e对应的第97个位置的数字+1。。。
        }
        for (int i = 0; i < str.length(); i++) {
            if (temp[str.charAt(i)] == 1) {  //求e对应的数组位置上的数值  str.charAt(i)从头开始遍历字符
                return i;
            }
        }

        return -1;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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