《LeetBook》leetcode题解(14):Longest Common Prefix[E]

时间:2023-03-09 22:44:02
《LeetBook》leetcode题解(14):Longest Common Prefix[E]

我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

《LeetBook》leetcode题解(14):Longest Common Prefix[E]

014.Longest Common Prefix[E]

问题

Write a function to find the longest common prefix string amongst an array of strings.

Subscribe to see which companies asked this question

思路

这个没啥思路的,怎么都要两重循环,因为是最长公共子串,随便找一个(一般是第一个作为基准),然后拿拿的首部慢慢去匹配后面的字符串就行了。

public class Solution {
public String longestCommonPrefix(String[] strs) {
String s = "";
if(strs.length == 0)
return "";
for(int i = 0; i < strs[0].length();i++)
{
char c = strs[0].charAt(i);
for(int j = 1;j < strs.length;j++)
{
if(i >= strs[j].length() || strs[j].charAt(i) != c)
return s;
}
s += c;
}
return s;
}
}