Write a function to find the longest common prefix string amongst an array of strings.
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
int j = 0
LeetCode解题:寻找最长公共前缀

本文介绍了一种解决LeetCode问题14的方法——寻找数组中字符串的最长公共前缀。通过水平扫描(Horizontal scanning)的方式,利用indexOf()函数逐个比较字符,直到找到最长公共前缀。代码示例解释了indexOf()的使用,并提供了其他可能的解题思路,如垂直扫描、分治和二分搜索。
订阅专栏 解锁全文
170

被折叠的 条评论
为什么被折叠?



