最长公共前缀
方法一:横向扫描
思路:
- 找到两个字符串的公共前缀
- 两个两个比较,一直比较到字符串数组结束
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String commonPre = strs[0];
for (int i = 1; i < strs.length; i++) {
commonPre = getCommonPrefix(commonPre, strs[i]);
if (commonPre.isEmpty()) {
break;
}
}
return commonPre;
}
private String getCommonPrefix(String str1, String str2) {
int len = Math.min(str1.length(), str2.length());
int i = 0;
while (i < len && str1.charAt(i) == str2.charAt(i)) {
i++;
}
return str1.substring(0, i);
}
}
方法二:纵向扫描
方法一是横向扫描,依次遍历每个字符串,更新最长公共前缀。另一种方法是纵向扫描。
- 纵向扫描时,从前往后遍历所有字符串的每一列,比较相同列上的字符是否相同,
- 如果相同则继续对下一列进行比较,
- 如果不相同则当前列不再属于公共前缀,当前列之前的部分为最长公共前缀。
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int length = strs[0].length();
int count = strs.length;
for (int i = 0; i < length; i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < count; j++) {
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}
方法三:二分查找
-
首先确定最短字符串的长度,
-
然后在0到最短字符串长度之间进行二分查找,判断每个位置上的字符是否为公共前缀。
-
如果是,则将低位指针向右移动,
-
否则将高位指针向左移动。最终得到的低位指针即为最长公共前缀的长度。
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int minLength = Integer.MAX_VALUE;
for (String str : strs) {
minLength = Math.min(minLength, str.length());
}
int low = 0, high = minLength;
while (low < high) {
int mid = (high - low + 1) / 2 + low;
if (isCommonPrefix(strs, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return strs[0].substring(0, low);
}
public boolean isCommonPrefix(String[] strs, int length) {
String str0 = strs[0].substring(0, length);
int count = strs.length;
for (int i = 1; i < count; i++) {
String str = strs[i];
for (int j = 0; j < length; j++) {
if (str0.charAt(j) != str.charAt(j)) {
return false;
}
}
}
return true;
}
}
方法四:树
这个方法是chatgpt给的,太长太离谱了。。看看就好。。
-
首先定义了一个TrieNode类,该类表示Trie树的节点。节点中包含了一个长度为26的TrieNode数组用来存储子节点,代表了26个小写字母。另外还有一个布尔型变量isEnd,用来标记该节点是否是一个单词的结尾。
-
在插入操作insert中,通过遍历单词的每一个字符,将字符映射到对应的索引,并判断该索引对应的子节点是否为空。如果为空,则创建一个新的子节点。然后,将当前节点指向该子节点。最后,在单词的最后一个字符的位置上,将isEnd置为true,表示该节点是一个单词的结尾。
-
在求解最长公共前缀的方法getLongestCommonPrefix中,使用一个StringBuilder对象prefix来存储公共前缀。从根节点开始,循环执行以下操作:统计子节点数量count和最后一个非空子节点的索引index。如果count不等于1或者当前节点是一个单词的结尾(isEnd为true),则退出循环。否则,将当前节点的索引对应的字符追加到prefix中,并将当前节点指向最后一个非空子节点。最后,返回prefix转换为字符串的结果。
-
最后,定义了一个Solution类,其中的longestCommonPrefix方法接收一个字符串数组strs作为输入。首先判断strs是否为空或者长度为0,如果是则返回空字符串。否则,创建一个Trie树的根节点root。然后,遍历strs数组中的每一个字符串,将其插入到Trie树中。最后,调用root的getLongestCommonPrefix方法来获取最长公共前缀,并将结果返回。
这段代码的时间复杂度为O(nm),其中n是strs数组的长度,m是所有字符串的平均长度。
class TrieNode {
private TrieNode[] children;
private boolean isEnd;
public TrieNode() {
children = new TrieNode[26];
isEnd = false;
}
public void insert(String word) {
TrieNode node = this;
for (char ch : word.toCharArray()) {
int index = ch - 'a';
if (node.children[index] == null) {
node.children[index] = new TrieNode();
}
node = node.children[index];
}
node.isEnd = true;
}
public String getLongestCommonPrefix() {
StringBuilder prefix = new StringBuilder();
TrieNode node = this;
while (true) {
int count = 0;
int index = 0;
for (int i = 0; i < 26; i++) {
if (node.children[i] != null) {
count++;
index = i;
}
}
if (count != 1 || node.isEnd) {
break;
}
prefix.append((char) ('a' + index));
node = node.children[index];
}
return prefix.toString();
}
}
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
TrieNode root = new TrieNode();
for (String str : strs) {
root.insert(str);
}
return root.getLongestCommonPrefix();
}
}
over~