由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - A simple interview question
相关主题
看到一个c的面试题,求教。最郁闷的facebook面试+面经。
G phone interview求G加一题的线性解法
补 ms onsite 面筋Google onsite 题目求助
问个《编程实践》(英文版)里面的问题请教一个bloomberg题目
看一道面试题这题有好办法吗?
上一道题吧问一道C++编程题
大家看看我写的这个itoa有没有bugfacebook phone面试题
这题in place 做不了吧?Facebook被拒,写个面经
相关话题的讨论汇总
话题: char话题: pfast话题: pslow话题: res话题: outstr
进入JobHunting版参与讨论
1 (共1页)
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 的大作中提到】
: 不会写程序,但是可以这么搞
: 读第一个 字母,
: 计数器=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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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;
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
Facebook被拒,写个面经看一道面试题
MS interview question上一道题吧
问个bit struct的面试题 急大家看看我写的这个itoa有没有bug
问个面试时候hash table的C++实现问题这题in place 做不了吧?
看到一个c的面试题,求教。最郁闷的facebook面试+面经。
G phone interview求G加一题的线性解法
补 ms onsite 面筋Google onsite 题目求助
问个《编程实践》(英文版)里面的问题请教一个bloomberg题目
相关话题的讨论汇总
话题: char话题: pfast话题: pslow话题: res话题: outstr