由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这小段code有什么问题吗?
相关主题
大牛来做一下这道题请教一道c/c++题 (转载)
被thank you的fb电面面经求教一个题目,sudoku 下面代码哪里错了。。。
两道面试题,请大家说说看法这两个edit distance的code
请教一个C++问题Linkedin 电面 面经x2
A家白板interview失败Pascal's Triangle II 的优化解?
请教一个Google 面试题来问一个关于smart pointer的弱问题
问个complexity问题leetcode ValidNumber问题的代码,供参考
fb面试题【转】一道leetcode变种,twitter常考,怎么做?
相关话题的讨论汇总
话题: input1话题: input2话题: char话题: const话题: return
进入JobHunting版参与讨论
1 (共1页)
s*****s
发帖数: 157
1
被问到的, 在 input1 中找 input2, 如果找到, 就从找到的位置返回剩余的input1.
问是否code是对的, 如何改进, 如何优化。
我觉得code是对的啊,
char* test( char* input1, const char* input2 )
{
while( *input1 )
{
char *a = input1, *b = input2;
while( (*a++ == *b++) && *a && *b );
if( *b == 0 )
return input1;
input1++;
}
return 0;
}
P*******b
发帖数: 1001
2
char -> const char
优化是不是把b放到a前面?

input1.

【在 s*****s 的大作中提到】
: 被问到的, 在 input1 中找 input2, 如果找到, 就从找到的位置返回剩余的input1.
: 问是否code是对的, 如何改进, 如何优化。
: 我觉得code是对的啊,
: char* test( char* input1, const char* input2 )
: {
: while( *input1 )
: {
: char *a = input1, *b = input2;
: while( (*a++ == *b++) && *a && *b );
: if( *b == 0 )

s*****s
发帖数: 157
3
其实code还是有错误, 比如
input1 is "abc", input2 is "b"
suppose to return "bc", but it returns "abc"
that "b++" is wrong...
s*****s
发帖数: 157
4
没人能找出更多错误?
K******g
发帖数: 1870
5
这个是错误吗?我怎么觉得返回就是“bc”呢?

【在 s*****s 的大作中提到】
: 其实code还是有错误, 比如
: input1 is "abc", input2 is "b"
: suppose to return "bc", but it returns "abc"
: that "b++" is wrong...

s*******t
发帖数: 248
6
input2 const char* -> char*
(*a++ == *b++) -> (*a == *b), then in the loop a++, b++.
then it will be fine.

input1.

【在 s*****s 的大作中提到】
: 被问到的, 在 input1 中找 input2, 如果找到, 就从找到的位置返回剩余的input1.
: 问是否code是对的, 如何改进, 如何优化。
: 我觉得code是对的啊,
: char* test( char* input1, const char* input2 )
: {
: while( *input1 )
: {
: char *a = input1, *b = input2;
: while( (*a++ == *b++) && *a && *b );
: if( *b == 0 )

s*****s
发帖数: 157
7
it is still wrong if you put 'a++, b++' inside the while(...){} loop
for example:
input1 is "abc"
input2 is ""
suppose to return null, it returns "abc" indeed.
s*****s
发帖数: 157
8
这个是错误, 返回不是 "bc", 你可以run 着试一下

【在 K******g 的大作中提到】
: 这个是错误吗?我怎么觉得返回就是“bc”呢?
j***i
发帖数: 1278
9
一开始还要排除 空指针把,
input1 也应该是const pointer 毕竟也不休改input1

【在 s*******t 的大作中提到】
: input2 const char* -> char*
: (*a++ == *b++) -> (*a == *b), then in the loop a++, b++.
: then it will be fine.
:
: input1.

s*****s
发帖数: 157
10
另外这个code是复杂度应该是 O(n^2), 不知道能不能改进
s*****s
发帖数: 157
11
如果让input1也是const, 那么 char * func() 也要是 const 的, 因为program中有
return input1;

【在 j***i 的大作中提到】
: 一开始还要排除 空指针把,
: input1 也应该是const pointer 毕竟也不休改input1

1 (共1页)
进入JobHunting版参与讨论
相关主题
一道leetcode变种,twitter常考,怎么做?A家白板interview失败
求一道题的解答请教一个Google 面试题
opt notice returned...问个complexity问题
C++ Q23: if if elsefb面试题【转】
大牛来做一下这道题请教一道c/c++题 (转载)
被thank you的fb电面面经求教一个题目,sudoku 下面代码哪里错了。。。
两道面试题,请大家说说看法这两个edit distance的code
请教一个C++问题Linkedin 电面 面经x2
相关话题的讨论汇总
话题: input1话题: input2话题: char话题: const话题: return