Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
Tags: String
题意是让你从字符串数组中找出公共前缀,我的想法是找出最短的那个字符串的长度 minLen
,然后在 0...minLen
的范围比较所有字符串,如果比较到有不同的字符,那么直接返回当前索引长度的字符串即可,否则最后返回最短的字符串即可。
class Solution {
public String longestCommonPrefix(String[] strs) {
int len = strs.length;
if (len == 0) return "";
int minLen = 0x7fffffff;
for (String str : strs) minLen = Math.min(minLen, str.length());
for (int j = 0; j < minLen; ++j)
for (int i = 1; i < len; ++i)
if (strs[0].charAt(j) != strs[i].charAt(j))
return strs[0].substring(0, j);
return strs[0].substring(0, minLen);
}
}
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:awesome-java-leetcode