题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
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;
}