首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一道面试题,大家有没有好的,给小弟我借鉴借鉴

2012-10-17 
一道面试题,大家有没有好的,给我借鉴借鉴现有一长度为n的字符串,请用任意语言编写函数:求出现最多的那个字

一道面试题,大家有没有好的,给我借鉴借鉴
现有一长度为n的字符串,请用任意语言编写函数:求出现最多的那个字母总共出现了多少次。

[解决办法]
a[26] = {0};
a[str[i] - 'a']++;

最后看一下a数组中哪个数据最大就好了
[解决办法]
1楼应该是正解,o(n)算法应该是最快的
[解决办法]

探讨
a[26] = {0};
a[str[i] - 'a']++;

最后看一下a数组中哪个数据最大就好了

[解决办法]
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){     char testString[20] = {0};     strcpy(testString,"glbRa dadAdadAglbR");     int i = 0;     int max = 0;     int a[26] = {0};     for(i=0;i<20;i++)     {        if(isalpha(testString[i]))        {            if(islower(testString[i]))            {                a[testString[i]-'a']++;            }            else            {                a[testString[i]-'A']++;            }        }     }     for(i=0;i<26;i++)     {        if(a[i] > max)            max = a[i];        printf("a[%d] = %d\n",i,a[i]);     }     printf("max = %d\n",max);     return 0;}
[解决办法]
Python code
str = "abcnashcduiyqwcceqoizpzczmclaqporwirkxnvxvcmlzkpqe"ret = "abcdefghijklmnopqrstuvwxyz"str = str.lower()a = [0 for x in range(26)]for i in range(len(str)):    a[ord(str[i]) - ord('a')] = a[ord(str[i]) - ord('a')] + 1max = reduce(lambda x, y: (x > y and [x, y] or [y, x])[0], a)print ret[a.index(max)], max#print reduce(lambda x, y: x + y, a)
[解决办法]
探讨

a[26] = {0};
a[str[i] - 'a']++;

最后看一下a数组中哪个数据最大就好了

[解决办法]
//该处理过程取得第一个出现次数做多的字符和其出现的次数,只有出现相同次数的其他字符没有考虑
int getcharcount(char ch, const char *pSrc)
{
int count = 0;
if(pSrc!=NULL)
{
char *p = (char*)pSrc;
while(*p!='\0')
{
if(*p++==ch)
{
count++;
}
}
}
return count;
}
bool bIsExist(char ch, const char *pSrc)
{
bool bIs = false;
if(pSrc != NULL)
{
char *p = (char*)pSrc;
while(*p!='\0')
{
if(ch == *p++)
{
bIs = true;
break;
}
}
}
return bIs;
}
//得到第一个出现次数做多的字符和其出现的次数 算法做了点点粗糙的优化
int getmaxcount(char &ch, const char* pSrc)
{
int max = 0;

if(pSrc != NULL)
{
char *pMem = new char[strlen(pSrc)];//用于存放已经比较过的字符

int iIndex = 0;
char *p = (char*)pSrc;
while(*p!='\0')
{
if(!bIsExist(*p, pMem))
{
max=max>getcharcount(*p, pSrc) ? max : ch=*p, getcharcount(*p, pSrc);
pMem[iIndex++]=*p;
}
p++;
}
delete []pMem;
}
return max;
}
int main()
{
char *pTemp="The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page, unless legacy standards or data formats prevent the use of Unicode. If use of Unicode is not possible, applications should tag the data stream with the appropriate encoding name when protocols allow it. HTML, XML, and HTTP files allow tagging, but text files do not.";
char pl;
int max = getmaxcount(pl, pTemp);//第一个出现次数做多的字符和其出现的次数


}
// 有不足之处还望不吝赐教!
[解决办法]

探讨

a[26] = {0};
a[str[i] - 'a']++;

最后看一下a数组中哪个数据最大就好了

[解决办法]
因为只有26个字母,所以定义数组num[26]就是。
然后num[str[i]-'a']++ ,最后看谁多。。


其实就是1楼;
[解决办法]
这个我看了很久都没明白 改了一点实现了
str = "abcnAshcduiyqwcceqoizpzczmCclaqporwirkxnvxvcmlzkpqe"
str=str.lower()
a={}
for i in range(len(str)):
a.setdefault(str[i],0)
a[str[i]]+=1
results=list(a.items())
results.sort(key=lambda tuple: tuple[1], reverse = True)
print results[0]

探讨

Python code

str = "abcnashcduiyqwcceqoizpzczmclaqporwirkxnvxvcmlzkpqe"
ret = "abcdefghijklmnopqrstuvwxyz"
str = str.lower()
a = [0 for x in range(26)]
for i in range(len(str)):
a[ord(str[i]) - ……

[解决办法]
探讨

因为只有26个字母,所以定义数组num[26]就是。
然后num[str[i]-'a']++ ,最后看谁多。。


其实就是1楼;

热点排行