美文网首页
387. 字符串中的第一个唯一字符

387. 字符串中的第一个唯一字符

作者: 六十年目裁判长亚玛萨那度 | 来源:发表于2019-01-10 10:35 被阅读0次

题目描述:给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

难度:简单

int find(char ch, char *s) {
    for (int i = 0; s[i]; i++) {
        if (s[i] == ch) return i;
    }
    return -1;
}


int firstUniqChar(char* s) {
    int code[30] = {0};
    int i = 0;
    for (int i = 0; s[i]; i++) {
        code[s[i] - 'a'] += 1;
    }
    for (int i = 0; s[i]; i++) {
        
        if (code[s[i] - 'a'] == 1) {
            //printf("%c ", i + 'a');
            //return find(i + 'a', s);
            return i;
        }
    }
    return -1;
}

相关文章

网友评论

      本文标题:387. 字符串中的第一个唯一字符

      本文链接:https://www.haomeiwen.com/subject/aumorqtx.html