贡献一个朋友在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);
}
如果答得不好,求教正确的做法。谢谢!
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);
}
如果答得不好,求教正确的做法。谢谢!