原题:https://leetcode-cn.com/problems/longest-common-prefix/

拿到第一个字符串,依次比较,逐渐削减自身。直到所有的字符都比较完成。

public class Solution {
    public string LongestCommonPrefix(string[] strs) {
        	if (strs.Length == 0) return "";
			String prefix = strs[0];
			for (int i = 0; i < strs.Length; i++)
			{
				while (strs[i].IndexOf(prefix)!=0)
				{
					prefix = prefix.Substring(0, prefix.Length - 1);
					if (string.IsNullOrWhiteSpace(prefix))
					{
						return "";
					}
				}
			}
			return prefix;
    }
}

c# LeetCode 14. 最长公共前缀 (string)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
猜你喜欢
  • 2020-01-01
  • 2021-06-30
  • 2021-04-02
  • 2021-06-01
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案