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

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

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")

全部评论

相关推荐

06-26 17:24
已编辑
宁波大学 Java
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
八股刚起步,看了javaguide,小林coding,还有面渣,感觉面渣是体验最好的,请问只看面渣够用吗,有不完善的需要补吗?
码农索隆:先背最基础的知识,然后理解情景题,现在面试大多数喜欢问情景题,更考验面试者的基础和临场发挥情况
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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