LeetCode刷題筆記 - 14. 最長(zhǎng)公共前綴
學(xué)好算法很重要,然后要學(xué)好算法,大量的練習(xí)是必不可少的,LeetCode是我經(jīng)常去的一個(gè)刷題網(wǎng)站,上面的題目非常詳細(xì),各個(gè)標(biāo)簽的題目都有,可以整體練習(xí),本公眾號(hào)后續(xù)會(huì)帶大家做一做上面的算法題。
官方鏈接:https://leetcode-cn.com/problemset/all/
一、題意
難度:簡(jiǎn)單
https://leetcode-cn.com/problems/longest-common-prefix/
編寫(xiě)一個(gè)函數(shù)來(lái)查找字符串?dāng)?shù)組中的最長(zhǎng)公共前綴。
如果不存在公共前綴,返回空字符串 ""。
示例
輸入:strs = ["flower","flow","flight"]
輸出:"fl"
輸入:strs = ["dog","racecar","car"]
輸出:""
解釋:輸入不存在公共前綴。
提示:
0 <= strs.length <= 2000 <= strs[i].length <= 200strs[i]僅由小寫(xiě)英文字母組成
二、解題
方法一:橫向掃描
思路:
依次遍歷字符串?dāng)?shù)組 將每?jī)蓚€(gè)字符串進(jìn)行比較,更新共同前綴
代碼:
class Solution {
public String longestCommonPrefix(String[] strs) {
// 邊界判斷
if(strs == null || strs.length == 0 || "".equals(strs[0])) return "";
// 取一個(gè)字符串做前綴用以判斷
StringBuilder pre = new StringBuilder(strs[0]);
// 遍歷后續(xù)字符串
for(int i = 1; i < strs.length; i++){
// 更新共同前綴
while(strs[i].indexOf(pre.toString()) != 0){
pre.deleteCharAt(pre.length() - 1);
if(pre.length() == 0) return "";
}
}
// 返回結(jié)果
return pre.toString();
}
}
復(fù)雜度分析:
時(shí)間復(fù)雜度:O(mn) 空間復(fù)雜度:O(1)
后續(xù)題解來(lái)源:https://leetcode-cn.com/problems/longest-common-prefix/solution/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/
方法二:縱向掃描
思路:
從前往后遍歷所有字符串的每一列,比較相同列上的字符是否相同 如果相同則繼續(xù)對(duì)下一列進(jìn)行比較 如果不相同則當(dāng)前列不再屬于公共前綴,當(dāng)前列之前的部分為最長(zhǎng)公共前綴。
代碼:
class Solution {
public String longestCommonPrefix(String[] strs) {
// 邊界判斷
if (strs == null || strs.length == 0) {
return "";
}
int length = strs[0].length();
int count = strs.length;
// 遍歷字符串?dāng)?shù)組
for (int i = 0; i < length; i++) {
char c = strs[0].charAt(i);
// 遍歷其余字符串同位元素
for (int j = 1; j < count; j++) {
// 如果不相同,則截取當(dāng)前列之前的部分
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
// 返回結(jié)果
return strs[0];
}
}
復(fù)雜度分析:
時(shí)間復(fù)雜度:O(mn) 空間復(fù)雜度:O(1)
方法三:分治
思路:
將相同的問(wèn)題分解成若干個(gè)小問(wèn)題 兩兩比較獲取共同前綴
代碼:
class Solution {
public String longestCommonPrefix(String[] strs) {
// 邊界判斷
if (strs == null || strs.length == 0) {
return "";
} else {
// 返回結(jié)果
return longestCommonPrefix(strs, 0, strs.length - 1);
}
}
public String longestCommonPrefix(String[] strs, int start, int end) {
// 邊界判斷
if (start == end) {
return strs[start];
} else {
// 分治
int mid = (end - start) / 2 + start;
String lcpLeft = longestCommonPrefix(strs, start, mid);
String lcpRight = longestCommonPrefix(strs, mid + 1, end);
// 統(tǒng)計(jì)前綴
return commonPrefix(lcpLeft, lcpRight);
}
}
public String commonPrefix(String lcpLeft, String lcpRight) {
int minLength = Math.min(lcpLeft.length(), lcpRight.length());
for (int i = 0; i < minLength; i++) {
// 兩兩比較字符串同位元素
if (lcpLeft.charAt(i) != lcpRight.charAt(i)) {
return lcpLeft.substring(0, i);
}
}
// 返回結(jié)果
return lcpLeft.substring(0, minLength);
}
}
復(fù)雜度分析:
時(shí)間復(fù)雜度:O(mn) 空間復(fù)雜度:O (mlogn)
方法四:二分查找
思路:
每次取查找范圍的中間值 mid,判斷每個(gè)字符串的長(zhǎng)度為 mid 的前綴是否相同 如果相同則最長(zhǎng)公共前綴的長(zhǎng)度一定大于或等于 mid 如果不相同則最長(zhǎng)公共前綴的長(zhǎng)度一定小于 mid 通過(guò)上述方式將查找范圍縮小一半,直到得到最長(zhǎng)公共前綴的長(zhǎng)度。
代碼:
class Solution {
public String longestCommonPrefix(String[] strs) {
// 邊界判斷
if (strs == null || strs.length == 0) {
return "";
}
int minLength = Integer.MAX_VALUE;
for (String str : strs) {
// 得到最短的字符串長(zhǎng)度
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;
}
}
// 返回結(jié)果
return strs[0].substring(0, low);
}
public boolean isCommonPrefix(String[] strs, int length) {
String str0 = strs[0].substring(0, length);
int count = strs.length;
// 遍歷后續(xù)字符串
for (int i = 1; i < count; i++) {
String str = strs[i];
// 兩兩比較當(dāng)前位置元素是否相等
for (int j = 0; j < length; j++) {
if (str0.charAt(j) != str.charAt(j)) {
return false;
}
}
}
return true;
}
}
復(fù)雜度分析:
時(shí)間復(fù)雜度: O(mnlogm) 空間復(fù)雜度:O (1)
- END -我的github上有大量計(jì)算機(jī)類相關(guān)電子書(shū)籍,都是通過(guò)網(wǎng)絡(luò)中眾多好心人分享所搜集到了,有需要的小伙伴可以到下面兩個(gè)地址自取哦~ https://github.com/unidentifiable/java-other-books https://gitee.com/unidentifiable/java-other-books
