using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ public string longestCommonPrefix (List<string> strs) { // write code here if (strs == null) return null; if (strs.Count == 0) return string.Empty; if (strs.Count == 1) return strs[0]; string strMin = strs[0]; for (int nIndex = 1; nIndex < strs.Count; nIndex++) { if (strs[nIndex].Length >= strMin.Length) continue; else strMin = strs[nIndex]; } if (string.IsNullOrWhiteSpace(strMin)) return ""; string strRtn = string.Empty; for (int nIndex = strMin.Length - 1; nIndex >= 0; nIndex--) { int nCount = 0; string strRtnTmp = strMin.Substring(0, nIndex + 1); for (int nIndexSec = 0; nIndexSec < strs.Count && strs[nIndexSec].Contains(strRtnTmp); nIndexSec++) nCount++; if (nCount == strs.Count) { strRtn = strRtnTmp; break; } } return strRtn; } }