查找第一个只出现一次的字符

 

#include "stdafx.h"
#include <string>
using namespace std;
#include <vector>
#include <map>

class Solution
{
public:
    int  find_right_char(string str)
    {
        int strLen = str.size();
        if (strLen ==0)
        {
            return -1;
        }
        map<char,int> item;
        for (size_t i = 0; i < strLen; i++)
        {
            item[str[i]] = item[str[i]] + 1;
        }

        for (size_t i = 0; i < strLen; i++)
        {
            if (item[str[i]] ==1)
            {
                return str[i];
            }
        }
        return -1;
    }
};



int main()
{
    Solution aa;
    string str = "12312";
    aa.find_right_char(str);
    return 1;
}

posted on 2020-04-14 23:04  心中日月007  阅读(123)  评论(0编辑  收藏  举报