您的位置:首页 > 其它

统计字符串中第一个出现次数为1的字符

2012-08-21 15:05 316 查看
用类似位图的办法,只是用数字实现。利用数组统计每个字符出现的次数,然后扫描第一个次数为1的元素

#include "stdafx.h"
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;

char findSingle(char *str)//一遍扫描法
{
int count[256]={0};//这里都是小写字母,所以大小是26,也可以设置为256

char *p = str;
while(*p != '\0')
{
count[*p]++;
p++;
}

for(int i=0;i<256;i++)
{
if(count[i] == 1)
{
return static_cast<char>(i);

}
}

}
int main()
{
char *str="fsdfasfdiarewtrewtrew";
cout<<findSingle(str)<<endl;
getchar();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: