c***2 发帖数: 838 | 1 Given a string "aaabbcccaa"
Output a3b2c3a2
restriction: scan the string only once. |
t******t 发帖数: 15246 | 2 不会写程序,但是可以这么搞
读第一个 字母,
计数器=1;
输出字符串=第一个字母
FOR
读当前字母;
如果计数器=0,
计数器=1;
输出字符串+当前字母
如果计数器》0且和上个字母相同,计数器+1
如果不同,将计数器当前数字加入输出字符串;
计数器清0;
结束,输出字符串 |
c***2 发帖数: 838 | 3 Never mind if you don't know programming. :-)
It's not trivial to write bug-free codes *quickly* for this simple question.
Try it.
【在 t******t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 不会写程序,但是可以这么搞 : 读第一个 字母, : 计数器=1; : 输出字符串=第一个字母 : FOR : 读当前字母; : 如果计数器=0, : 计数器=1; : 输出字符串+当前字母 : 如果计数器》0且和上个字母相同,计数器+1
|
f*********8 发帖数: 34 | 4 我写程序能力很差,大牛轻拍,我觉得我的程序没有满足scan the string only once
的要求,因为总是access str[i-1]了
void main()
{
char str[]="aaabccffeeeehhyu";
int i=0,j=0;
int count=0;
char tmp=str[0];
char outstr[20];
char outstrcount[2];
while(str[i])
{
if(str[i]==tmp)
{
count+=1;
tmp=str[i];
i++;
}
else
{
outstr[j++]=str[i-1];
itoa(count,outstrcount,10);
outstr[j++]=outstrcount[0];
tmp=str[i];
i++;
count=1;
}
}
outstr[j++]=str[i-1];
itoa(count,outstrcount,10);
outstr[j++]=outstrcount[0];
outstr[j]='\0';
printf("%s",outstr);
}
|
c***2 发帖数: 838 | 5 I am not a 大牛. :-)
Forget to mention, don't have to put the results in a buf, just print it.
Here's my solution on the scene:
(The interviewer is quite satisfied :-) |
f****g 发帖数: 313 | 6 The following is my code :S
#include
#include
#include
#define MAXLEN 5
char* countCharInStr(const char* s,
unsigned int len)
{
char *pFast, *pSlow;
unsigned char num[MAXLEN] = {0};
unsigned int count = 0;
unsigned int resC = 0;
if( 0 == len || NULL == s)
{
return NULL;
}
char *res = (char*)malloc(sizeof(char)*len);
if( NULL == res)
{
return NULL;
}
pSlow = s;
pFast = pSlow + 1;
while( *pFast != 0)
{
if(*pFast == *pSlow)
{
count ++;
pFast ++;
}
else
{
res[resC++] = *pSlow;
int dec = sprintf(res+resC, "%d", count + 1);
resC += dec;
pSlow = pFast;
count = 0;
pFast ++;
}
}
res[resC++] = *pSlow;
int dec = sprintf(res+resC, "%d", count + 1);
resC += dec;
res[resC] = 0;
return res;
} |
f*********8 发帖数: 34 | 7 真是简洁巧妙啊,学到了,多谢,:)
【在 c***2 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I am not a 大牛. :-) : Forget to mention, don't have to put the results in a buf, just print it. : Here's my solution on the scene: : (The interviewer is quite satisfied :-)
|
f****g 发帖数: 313 | 8 这个Code写得很简单漂亮啊:D
【在 c***2 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I am not a 大牛. :-) : Forget to mention, don't have to put the results in a buf, just print it. : Here's my solution on the scene: : (The interviewer is quite satisfied :-)
|
i**********e 发帖数: 1145 | 9 nice solution, i like it
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
【在 c***2 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I am not a 大牛. :-) : Forget to mention, don't have to put the results in a buf, just print it. : Here's my solution on the scene: : (The interviewer is quite satisfied :-)
|
l**t 发帖数: 64 | 10 还可以更简洁一点
void foo(const char* s)
{
assert(s);
while (char c=*s) {
int n = 1;
while (c == *(++s)) ++n;
printf("%c%d", c, n);
}
}
【在 c***2 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I am not a 大牛. :-) : Forget to mention, don't have to put the results in a buf, just print it. : Here's my solution on the scene: : (The interviewer is quite satisfied :-)
|
b*****e 发帖数: 474 | 11 with no nested loop:
...
{
int n=0;
char c = *s;
while (*s)
if ( c==*s++ ) n++;
else {
printf("%c%d",c,n);
c=*s;
n=1;
}
} |