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

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

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

#include <bits/stdc++.h>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    int symbol =0;
    for (int i = 0; i < str.length(); i++) {
        int count =0;
        if (i == str.length() - 1) {
            for (int j = 0; j < str.length() - 1; j++) {
                if (str[i] == str[j]) {
                    count =1;
                    break;
                }
            }
            if (count == 0) {
                cout << str[i] << endl;
                symbol =1;
            }
        } 
        else {
            for (int j = str.length() -1; j >=0 && j != i; j--) {
                if (str[i] == str[j]) {
                    count =1;
                    str.erase(j,1);
                }
            }
            if (count == 0) {
                cout << str[i] << endl;
                symbol =1;
                break;
            }
        }
    }
    if(symbol ==0){
        cout << -1 << endl;
    }
}

全部评论

相关推荐

11-13 10:17
门头沟学院 Java
昨天面美团,jvm,juc问的好深啊,感觉小林coding不太够喔,牛油们有没有什么推荐的八股网站嘛🕒&nbsp;岗位/面试时间👥&nbsp;面试题目🤔&nbsp;面试感受
明天不下雨了:小林Coding:https://xiaolincoding.com/ 全栈哥:https://www.pdai.tech/ Guide哥:https://javaguide.cn/ 秀哥:https://interviewguide.cn/ 沉默王二:https://javabetter.cn/home.html 磊哥:https://www.javacn.site/interview/basic/ 小傅哥:https://bugstack.cn/ 源码哥:https://doocs.github.io/source-code-hunter/#/ 各大厂的公众号技术文章和一些经典的书籍
面试太紧张了怎么办?
点赞 评论 收藏
分享
牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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