码迷,mamicode.com
首页 > 其他好文 > 详细

第一个只出现一次的字符

时间:2019-12-06 19:30:41      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:字符串长度   字母   大小   int   include   public   turn   ring   scribe   

题目描述

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
#include <map>
class Solution {
public:
    map<char,int> mp;
    int FirstNotRepeatingChar(string str) {
        int len = str.size();
        if(len==0) return -1;
        for(int i =0;i<len;i++){
            if(mp.find(str[i]) != mp.end()) ++mp[str[i]];
            else mp[str[i]]=1;
        }
        if(mp.empty()) return -1;
        else for(int i =0;i<len;i++){
            if(mp[str[i]]==1) return i;
        }
        return -1;
    }
};

第一个只出现一次的字符

标签:字符串长度   字母   大小   int   include   public   turn   ring   scribe   

原文地址:https://www.cnblogs.com/decq/p/11997005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!