14.|14. Longest Common Prefix

14.|14. Longest Common Prefix
文章图片
题目.png
14.|14. Longest Common Prefix
文章图片
Algorithm.png 【14.|14. Longest Common Prefix】思路:
S1,S2计算出最长字符串前缀prefix,prefix与S3计算出新的prefix前缀,依次算出最后的prefix。

public static String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); } } return prefix; }

    推荐阅读