【leetcode】第一个只出现一次的字符
char firstUniqChar(char* s){
int hash[26] = {0};
int len = strlen(s); // 这里先求出strlen下面直接用,不建议再for循环的条件中写strlen,效率低了很多
for (int i = 0; i < len; ++i) {
hash[s[i] - 'a']++;
}
for (i = 0; i < len; ++i) {
if (hash[s[i] - 'a'] == 1) {
return s[i];
}
}
return ' ';
}
char firstUniqChar(char* s){
int count[26]={0};
int arr[26]={0};
int index = 0;
for (int i=0; i<strlen(s); i++)
{
count[s[i]-97] += 1;
if (index <= 25)
{
for (int j=0; j<= index; j++)
{
if (s[i] == arr[j])
break;
if (j == index)
{
arr[index++] = s[i];
break;
}
}
}
}
for (int k=0; k<index; k++)
{
for (int l=0; l<26; l++)
{
if (arr[k] - 97 == l)
{
if (count[l] == 1)
return arr[k];
break;
}
}
}
return 0x20;
}