第一次只出现一次的字符

来源:互联网 发布:sopcast2016地址源码 编辑:程序博客网 时间:2024/06/07 20:07

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置。若为空串,返回-1

【注意一定要考虑到各个方面:字符串是否为空,是否每个字母都出现了两次,等等】

class Solution {public:    int FirstNotRepeatingChar(string str) {        int len=str.length();        if(-1==len) //一定要考虑周全呀~~~~            return -1;        int i =0;        int* count=new int[256];        memset(count,0,sizeof(int)*256);        for(i=0;i<len;++i)        {            count[str[i]]++;        }        for(i=0;i<len;++i)        {            if(1==count[str[i]])            {                break;            }        }        delete[] count;        if(i==len) //考虑周全的重要性啊~~~            return -1;        return i;            }};


0 0
原创粉丝点击