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

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

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


using namespace std;

char first_Char(string s)
{
    map<char,int> m;
    for(int i=0;i<s.size();i++)
    {
        m[s[i]]++;
    }
    
    set<char> v;
    for(auto c:m)
    {
        if(c.second==1)
        {
            v.insert(c.first);
        }
    }
    //cout<<v.size()<<endl;
    char res;
    if(v.size()==0)
    {
        res='-1';
    }
    else{
        for(auto c:s)
        {
            if(v.find(c)!=v.end())
            {
                res=c;
                break;
            }
        }
    }
    return res;
}

int main()
{
    string s;
    while(cin>>s)
    {
        char res=first_Char(s);
        if(tolower(res)>='a'&&tolower(res)<='z')
        {
            cout<<res<<endl;
        }
        else
        {
            cout<<-1<<endl;
        }
    }
    return 0;
}
全部评论

相关推荐

醉蟀:你不干有的是人干
点赞 评论 收藏
分享
06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务