在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b

    xiaoxiao2021-03-26  13

    #include <stdio.h> #include <stdlib.h> #include <string.h> /* 题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b 解法:考虑用一个hash表记录每个字符出现的次数,然后顺序遍历字符串,同时判断hash值是否为1 */ int main() { char str[99]="abaccdeff"; int strLength=strlen(str); int hashTable[256]={0}; for(int i=0;i<strLength;++i){ hashTable[str[i]]++; } for(int j=0;j<strLength;++j){ if(hashTable[str[j]]==1){ printf("%c",str[j]); break; } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-500003.html

    最新回复(0)