Redian新闻
>
贡献一个朋友在Google的面题一枚。
avatar
贡献一个朋友在Google的面题一枚。# JobHunting - 待字闺中
m*m
1
Interview Questions:
Redefine a function (signature given) to write data to a new replacement for
an antiquated database (which you previously designed)
Answer Question:
Write a function to return the longest common prefix between two strings.
//java code
String GetCommonPrefix(String a, String b)
{
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
int startIndex = 0;
//choose short length as the end index
int needLength= aChar.length>bChar.length?bChar.length:aChar.length;
while(startIndex{
if(aChar[startIndex]!=bChar[startIndex])//find different!
break;
else
startIndex++;
}
return a.subString(0, startIndex);
}
// longest prefix
public String longestPrefix(String s1, String s2) {
int iCommon = 0;
for (int i = 0; i <= Math.min(s1.length(),s2.length());i++) {
if (s1.charAt(i) != s2.charAt(i)) {
break;
}else {
iCommon++;
}
}
return s1.substring(0,iCommon);
}
如果答得不好,求教正确的做法。谢谢!
avatar
p*2
2
i <= Math.min(s1.length(),s2.length())
<=?
avatar
w*o
3
One mistake in the code:
//java code
String GetCommonPrefix(String a, String b)
{
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
int startIndex = 0;
//choose short length as the end index
int needLength= aChar.length>bChar.length?bChar.length:aChar.length;
while(startIndex{
if(aChar[startIndex]!=bChar[startIndex])//find different!
break;
else
startIndex++;
}
return a.subString(0, startIndex);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
should be return a.subString(0, startIndex-1);

for

【在 m*******m 的大作中提到】
: Interview Questions:
: Redefine a function (signature given) to write data to a new replacement for
: an antiquated database (which you previously designed)
: Answer Question:
: Write a function to return the longest common prefix between two strings.
: //java code
: String GetCommonPrefix(String a, String b)
: {
: char[] aChar = a.toCharArray();
: char[] bChar = b.toCharArray();

avatar
d*u
4
for
这个没看懂,是让干嘛啊?
avatar
p*2
5

are you sure?

【在 w****o 的大作中提到】
: One mistake in the code:
: //java code
: String GetCommonPrefix(String a, String b)
: {
: char[] aChar = a.toCharArray();
: char[] bChar = b.toCharArray();
: int startIndex = 0;
: //choose short length as the end index
: int needLength= aChar.length>bChar.length?bChar.length:aChar.length;
: while(startIndex
avatar
s*o
6
looks like Java's subString specifies the begin and end index,
while C++ string's substring specifies begin index and length.

【在 p*****2 的大作中提到】
:
: are you sure?

avatar
r*b
7
I guess the second question is on the longest common substr of two strings.
Otherwise, why we need the second function?
Here is a post that has the pseudo-code on computing the longest common
substr:
http://basicalgos.blogspot.com/2012/03/17-longest-common-substr

for

【在 m*******m 的大作中提到】
: Interview Questions:
: Redefine a function (signature given) to write data to a new replacement for
: an antiquated database (which you previously designed)
: Answer Question:
: Write a function to return the longest common prefix between two strings.
: //java code
: String GetCommonPrefix(String a, String b)
: {
: char[] aChar = a.toCharArray();
: char[] bChar = b.toCharArray();

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。