华为oj--字符个数统计(不重复字符个数统计)**

计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。

输入:abcd

输出:4

 

例如2:

输入:abcdee

输出:5

方法一:set(注意包含字母大小写);测试不通过,时间太长

#include <stdlib.h>
#include "oj.h"
#include<set>
using namespace std;

/*
功能:

输入:字符串

输出:无
     
返回:字符个数
*/

int GetCount( char* strInValue )
{
	if (NULL== strInValue) {
		return -1;
	}

	int num[128];
	set<char> s;
	while (*strInValue!='\0') {
		if (*strInValue>=0&& *strInValue<=127) {
		
			s.insert(*strInValue);
		}
	}
	return  s.size();
}

方法二:

#include <stdlib.h>
#include "oj.h"
#include <string.h>
/*
功能:

输入:字符串

输出:无
     
返回:字符个数
*/

int GetCount( char* strInValue )
{
	if (NULL== strInValue) {
		return -1;
	}

	int char_num = 0; 
	int i = 0; 
	char *temp = strInValue; 
	char *rpt; 
	while (*temp!='\0') 
	{ 
		if (*temp >= 0 && *temp <= 127)
		{ 
			rpt = (char*)memchr(strInValue, *temp, (i + 1)); 
			if ((rpt - strInValue) == i) char_num++; 
		} 
		temp++; 
		i++; 
	} 
	return char_num;
}

猜你喜欢

转载自blog.csdn.net/nameix/article/details/80185058