最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
https://leetcode-cn.com/problems/longest-common-prefix/
如果不存在公共前缀,返回空字符串 “”。
题目描述详见 https://leetcode-cn.com/problems/longest-common-prefix/。
【解题思路】: 找出当前字符数组的最小串,它是理论上当前字符数组会出现的最长公共前缀,以它为基准,把字符数组遍历,引入位置指针,每次比较当前字符的第index位置是否都相等,如果相等,index指针后移一位,如果不相等,则相同的串为index之前。记得特殊情况处理,如空字符,如只有一个字符串等。
【程序代码】:详见如下代码。
package com.demo.course.mid.problem06;
/**
* author: nitaotao
* date: 2021/12/22 10:52
* version: 1.0
* description: https://leetcode-cn.com/problems/longest-common-prefix/
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
String[] str2 = {"ab", "aa"};
System.out.println(solution.longestCommonPrefix(str2));
}
/**
* @param strs
* @return
*/
public String longestCommonPrefix(String[] strs) {
for (int i = 0; i < strs.length; i++) {
if (strs[i].equals("")) {
return "";
}
}
if (strs.length == 1) {
return strs[0];
}
boolean isCommonPrefix = true;
int index = 0;
String minString = strs[0]; //当前数组里最大公共串,即最小串
for (int i = 0; i < strs.length; i++) {
if (strs[i].length() < minString.length()) {
minString = strs[i];
}
}
//先把所有元素的第一个字母拿出来比较
while (isCommonPrefix) {
for (int i = 0; i < strs.length; i++) {
if (strs[i].charAt(index) == minString.charAt(index)) {
//如果相等,判断下一个元素的该位置元素
continue;
} else {
isCommonPrefix = false;
break;
}
}
//如果当前位置相等,并且当前位置索引没到 minString的最后一位
if (isCommonPrefix && index < minString.length() - 1) {
//比较的指针向后移一位
index++;
} else {
//如果第一个位置就不相等
if (index == 0 && !isCommonPrefix) {
return "";
}
//如果当前位置相等,并且当前指针已经到最后一位了
if (isCommonPrefix) {
//返回 misString
return minString;
}
break;
}
}
//如果当前位置不相等了,说明之前所有位置都相等
return minString.substring(0, index);
}
}
【运行结果】:详见如下截图。