- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
华为笔试算法题汇总
华为2014笔试算法题汇总[cpp] view plaincopy
//////////////////////////////////////////////////////////////////////////
#include?iostream????
#include?cassert????
????
using?namespace?std;????
????
bool?g_flag[26];????
void?stringFilter(const?char?*pInputStr,?long?lInputLen,?char?*pOutputStr)????
{????
??assert(pInputStr?!=?NULL);????
??int?i?=?0;????
??if?(pInputStr?==?NULL?||?lInputLen?=?1)????
??{????
??????return;????
??}????
??const?char?*p?=?pInputStr;????
??while(*p?!=?\0)????
??{????
?????if?(g_flag[(*p?-?a)])????
?????{????
?????????p++;????
?????}else{????
?????????pOutputStr[i++]?=?*p;????
?????????g_flag[*p?-?a]?=?1;????
?????????p++;????
?????}????
??}????
??pOutputStr[i]?=?\0;????
}????
int?main()????
{????
????memset(g_flag,0,sizeof(g_flag));????
????char?input[]?=?abacacde;????
????char?*output?=?new?char[strlen(input)?+?1];????
????stringFilter(input,strlen(input),output);????
????coutoutputendl;????
????delete?output;????
????return?0;????
}????
2.
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1、仅压缩连续重复出现的字符。比如字符串abcbc由于无连续重复字符,压缩后的字符串还是abcbc。
2、压缩字段的格式为字符重复的次数+字符。例如:字符串xxxyyyyyyz压缩后就成为3x6yz。
要求实现函数:?
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr:? 输入字符串
??????????? lInputLen:? 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例?
输入:“cccddecc”?? 输出:“3c2de2c”
输入:“adef”???? 输出:“adef”
输入:“pppppppp” 输出:“8p”
[cpp] view plaincopy
//////////////////////////////////////////////////////////////////////////???
#include?iostream????
#include?cassert????
????
using?namespace?std;????
????
void?stringZip(const?char?*pInputStr,?long?lInputLen,?char?*pOutputStr)????
{????
??const?char?*p?=?pInputStr;????
??int?num?=?1;????
??int?i?=?0;????
??p++;????
??while(*p?!=?NULL)????
??{????
??????while(*p?==?*(p-1)?*p?!=?NULL)????
??????{????
???????num++;????
???????p++;????
??????}????
??????if?(num??1)????
??????{????
???????????int?size?=?0;????
???????????int?temp?=?num;????
?
文档评论(0)