题目:
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”
题解:
对每一个字符串,从头开始依次对比判断,直到最短的字符串长度。
string temp="";
if(strs.Length==0)
return temp;
int i = 0;
i = strs.GetLength(0);
int MIN = strs[0].Length ;
for(int j=1;j<i;j++)
{
if (MIN > strs[j].Length)
MIN = strs[j].Length;
}
int T = 1;
for (int j = 0; j < MIN; j++)
{
int K = 1;
for (int k=1;k<i;k++)
{
if (strs[k][j] == strs[k - 1][j])
K++;
else
return temp;
}
if (K == i)
temp = strs[0].Substring(0, T++);
}
return temp;