编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
using BundlerMinifier;
using System;
using System.IO;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace 水汐
{
public class Program
{
public static void Main(string[] args)
{
Program a = new Program();
string[] strs = { "flower","flows","flo" };
Console.WriteLine(a.LongestCommonPrefix(strs));
}
public string LongestCommonPrefix(string[] strs)
{
int number = 0;
try {
for (int j = 1; j < strs.Length; j++)
{
int ln = strs[j].Length;
if (strs[0].Length < ln) ln = strs[j - 1].Length;
for (int i = 0; i < ln; i++)
{
if (strs[j][i] == strs[0][i])
{
number = i;
}
else {
if (i == 0) number = -1;
break;
}
}
}
} catch (Exception e) { }
return strs[0].Substring(0, number + 1);
}
}
}