LeetCode OJ-387. First Unique Character in a String

来源:互联网 发布:nginx加lua模块 编辑:程序博客网 时间:2024/06/05 05:23
387. First Unique Character in a String

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"return 0.s = "loveleetcode",return 2.

Note: You may assume the string contain only lowercase letters.

利用辅助记录的数组判重就行了,注意空串情况,代码如下:
int firstUniqChar(char* s) {    int res = -1;    int rec[26] = { 0 };    int len = (int) strlen(s);    int i;    for (i = 0; i < len; ++i) {        ++rec[(int) (s[i] - 'a')];    }        for (i = 0; i < len; ++i) {        if (rec[(int) (s[i] - 'a')] == 1) {            res = i;            break;        }    }        return res;}


0 0
原创粉丝点击