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

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

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

纯C哈希表

!秒杀!

#include<stdio.h>
int main(void)
{
    char str[1000] = {0};
    int asc = 0;
    char temp = 0;
    int flag = 0;
    while(gets(str))
    {
        char tmp[1000] = {0};//非重复数
        char hash[1000] = {0};//重复数
        int len = strlen(str);
        for(int i = 0;i < len;i++)
        {
            temp = str[i];
            asc = (int)temp;
            if(tmp[asc] == 0)//第一次出现的数加入表tmp
            {
                tmp[asc] = 1;
            }
            else
                hash[asc] = 1;//重复出现的数加入表hash
        }
        for(int i = 0;i < len; i++)
        {
            temp = str[i];
            asc = (int)temp;
            if(hash[asc] == 0)//输出前与重复数表hash进行比对
            {
                printf("%c\n",str[i]);
                flag = 1;//设置标签
                break;
            }
        }
        if(flag == 0)//标签没动就输出-1
        {
            printf("-1\n");
        }
        flag = 0;//谨防多组输入输出
    }
}
全部评论

相关推荐

永联 dsp工程师 15k*15 双非硕士
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务