题目:
题解:
struct hashTable {
int key;
int val;
UT_hash_handle hh;
};
int firstUniqChar(char* s) {
struct hashTable* position = NULL;
int que[26][2], left = 0, right = 0;
int n = strlen(s);
for (int i = 0; i < n; ++i) {
int ikey = s[i];
struct hashTable* tmp;
HASH_FIND_INT(position, &ikey, tmp);
if (tmp == NULL) {
tmp = malloc(sizeof(struct hashTable));
tmp->key = ikey;
tmp->val = i;
HASH_ADD_INT(position, key, tmp);
que[right][0] = ikey;
que[right++][1] = i;
} else {
tmp->val = -1;
while (left < right) {
int ikey = que[left][0];
struct hashTable* tmp;
HASH_FIND_INT(position, &ikey, tmp);
if (tmp == NULL || tmp->val != -1) {
break;
}
left++;
}
}
}
return left < right ? que[left][1] : -1;
}