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

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

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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() 
{
    string s;
    cin >>s;
    int code[26];
    for(int i=0; i<26; ++i)
        {
            code[i] = 0;
        }
    for(auto c: s)
        {
            ++code[c-'a'];
        }
    for(auto c:s)
        {
            if(code[c-'a'] == 1) 
                {
                    cout << c << endl;
                    return 0;
                  }

        }
    cout << -1 << endl;
    return 0;
        

}

全部评论

相关推荐

头像
05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务