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

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

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

#include <iostream>
#include <string>

using namespace std;
int main()
{
    string str;
    char result;
    bool flag = false;
    while(cin>>str)
    {
        int length = str.length();
        for(int i=length -1 ; i>=0; i--)
        {
            int pos = str.find(str[i]);
            if( pos == i)
            {

                if(str.find(str[i], i+1) == str.npos)
                {
                     result = str[i];
                     flag = true;   
                }
            }
        }
        if(flag)
        {
            cout << result <<endl;
            flag = false;
        }
        else
        {
            cout<<"-1"<<endl;
        }
    }

    return 0;
}
全部评论

相关推荐

09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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