Write a function to find the longest common prefix string amongst an array of strings.
返回某字符串数组中最大的公共前缀字符串,比如(“my”,“myha"," myfdafd") 返回"my"
思路:根据字符串数组首字符串进行遍历,后续字符串中凡是与之字符相同便返回该字符串子串,否则返回首字符串。
public class Solution {
public string LongestCommonPrefix(string[] strs) {
if(strs!=null&&strs.Length==0)
{
return "";
}
for(int i=0;i<strs[0].Length;i++)
{
for(int j=1;j<strs.Length;++j)
{
if(strs[j].Length<=i||strs[j][i]!=strs[0][i])
{
return strs[0].Substring(0,i);
}
}
}
return strs[0];
}
}