题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream> #include <vector> using namespace std; int main() { string s; int min=1001; int count = 1; char res ; vector<int> store(26,0); getline(cin,s); for(int i = 0;i<s.size();i++){ if(store[s[i]-'a']==0){ store[s[i]-'a'] = count; count++; continue; }if(store[s[i]-'a']!=0){ store[s[i]-'a']=-1; count++; continue; } } for(int i = 0;i<26;i++){ if(store[i]<min){ if(store[i] == 0 || store[i] == -1){ continue; }else{ min = store[i]; res = 'a'+i; } } } if(min == 1001){ cout<<"-1"; }else{ cout<<res; } } // 64 位输出请用 printf("%lld")