在一个字符串中找到第一个只出现一次的字符(17)

来源:互联网 发布:淘宝全球购官网 编辑:程序博客网 时间:2024/05/17 04:58

第17题:
题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。 
分析:这道题是2006年google的一道笔试题。

 

/*  Name:   Copyright:   Author:   Date: 15-06-11 12:05  Description: :在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b*/#include<iostream>#include<iomanip>using namespace std;int main(){      char s[]="abbadsSsfayjuioyopgaAWDDERRGTHRccdeff";    int a[150];    memset(a,0,sizeof(int)*150);    const int len=strlen(s);    int t;    for(int i=0;i<len;i++)    {       a[s[i]]++;    }    for(int i=0;i<len;i++)    {        if(a[s[i]]==1)          {          cout<<s[i]<<endl;          break;          }    }    system("pause");    return 0;    }

 

分析:算法的复杂度为O(n)。开辟的整数数组大小为150,利用字符的ASCII码做索引。


 

原创粉丝点击