题解 | #找出字符串中第一个只出现一次的字符#

找出字符串中第一个只出现一次的字符

http://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

#include <stdio.h>

int main()
{
    char str[1000];
    while(scanf("%s", str) != EOF)
    {
        int num[1000] = {0};
        int len = strlen(str);
        int i, j;
        for(i = 0; i < len; i++)
        {
            num[str[i]-'0']++;
        }
        for(j = 0; j < len; j++)
        {
            if(num[str[j]-'0'] == 1)
            {
                printf("%c\n",str[j]);
                break;
            }
        }
        if(j >= len)
        {
            printf("-1\n");
        }
    }
    return 0;
}
全部评论

相关推荐

3 收藏 评论
分享
牛客网
牛客企业服务