https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

 

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
 

代码

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        int tms[256];
        // firstly, it's wrong without this sentence
        memset(tms, 0, sizeof(tms));
        
        for (int i=0; i<str.size(); ++i) {
            int tmp = int(str[i]);
            tms[tmp] = tms[tmp]+ 1;
        }
        for (int i=0; i<str.size(); ++i) {
            int tmp = int(str[i]);
            if (tms[tmp] == 1) return i;
        }
        return -1;
    }
};