打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
第一个只出现一次的字符
#include<iostream>#include<stdlib.h>using namespace std;//O(n^n)的时间复杂度char FirstNotRepeatingChar2(char *pString){    //如果是空指针,返回\0    if(pString==NULL)        return '\0';    int len=strlen(pString);    for(int i=0;i<len;i++)    {        int flag=0;//标识位,0表示这个字符只出现一次。        for(int j=i+1;j<len;j++)        {            if(pString[i]==pString[j])            {                flag=1;//1表示在当前字符后面存在于该字符相同的字符。            }        }        if(flag==0)            return pString[i];    }    return '\0';}//O(n)的时间复杂度char FirstNotRepeatingChar(char *pString){    //如果是空指针,返回\0    if(pString==NULL)        return '\0';    //定义hash表长度256,并创建哈希表    const int len=256;    int hashtable[len];    for(int i=0;i<len;i++)    {        hashtable[i]=0;    }    char *pHashkey=pString;    //第一遍遍历字符串,求出每个字符出现的次数    while((*pHashkey)!='\0')    {        hashtable[*(pHashkey++)]++;    }    pHashkey=pString;    //第二遍遍历字符串,求出第一个只出现一次的字符,每次都是按照字符串的顺序遍历    while((*pHashkey)!='\0')    {        if(hashtable[*pHashkey]==1)            return *pHashkey;        pHashkey++;    }    return '\0';}void main(){    char *pString="abaccdeff";    //cout<<pString<<endl;    //cout<<pString[1]<<endl;    cout<<sizeof(pString)<<endl;//4    cout<<strlen(pString)<<endl;//9    cout<<FirstNotRepeatingChar(pString)<<endl;    cout<<FirstNotRepeatingChar2(pString)<<endl;    system("pause");}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
比较sizeof()与strlen()
一个比较全的string类的实现(比较乱)
178 f0603
0~9(int型)与‘0’~‘9’(char型)的转换
字符指针赋值、传值
C++ String
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服