LeetCode刷题Java版014. 最长公共前缀
作者:沉默王二
发布时间:2022-07-28T14:35:42.685+0800
014. 最长公共前缀
题意
编写一个方法来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。
难度
简单
示例
输入:strs = ["flower","flow","flight"]
输出:"fl"
分析
这题并不难,只需要统计strs[i][j](字符串数组中每个字符串的每个字符)在j位置出现的次数即可,如果它出现了strs.length 次(字符串数组的长度),而且它的前缀也出现了strs.length ,那么它就是整个字符串数组的公共前缀。
所以我们只需要遍历这个字符数组,并且记录其相应的位置即可。
class Solution{
private int[][] cnt = new int[210][30];
public String longestCommonPrefix(String[] strs) {
for (String str : strs) {
for (int j = 0; j < str.length (); j++)
cnt[j][str.charAt(j) - 'a']++;
}
StringBuilder res = new StringBuilder();
for(int i = 0;i < strs[0].length();i++){
if(cnt[i][strs[0].charAt(i) - 'a'] != strs.length )
break;
res.append (strs[0].charAt(i));
}
return res.toString();
}
}
上述的方法相当于把整个字符串数组从左到右,从上到下依次扫描了一遍,有些没必要的遍历仍然跑了一遍,所以造成了一定的冗余。
能不能优化一下呢?
我们可以通过不断求strs[i]和strs[i + 1]的公共前缀,然后依次向后判断来加速,从而避免了后面的无谓的判断,虽然算法时间复杂度并没有变化,都是,但是因为少了一些无谓的地方的统计,可以使程序的执行效率进一步提升。
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
String pre = strs[0];
for(int i = 1;i < strs.length ;i++)
while (strs[i].indexOf(pre) != 0)
pre = pre.substring(0, pre.length () - 1);
return pre;
}
}
首先,默认strs[0]为公共前缀,然后依次将其缩短(截取掉最后一个字符),不断求解与strs[i]的公共前缀。最终就可以得到所有strs[i]的公共前缀。
总结
虽然我们最开始提出的算法就能实现我们的目的,但是通过去除冗余的一些统计,我们仍然能找到可以优化的地方。所以不断摈弃落后的,冗余的,不仅仅是自然界的物竞天择,也是我们设计更快算法的核心理念。
力扣链接:https://leetcode.cn/problems/longest-common-prefix/
014. 最长公共前缀
题意
编写一个方法来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。
难度
简单
示例
输入:strs = ["flower","flow","flight"]
输出:"fl"
分析
这题并不难,只需要统计strs[i][j](字符串数组中每个字符串的每个字符)在j位置出现的次数即可,如果它出现了strs.length 次(字符串数组的长度),而且它的前缀也出现了strs.length ,那么它就是整个字符串数组的公共前缀。
所以我们只需要遍历这个字符数组,并且记录其相应的位置即可。
class Solution{
private int[][] cnt = new int[210][30];
public String longestCommonPrefix(String[] strs) {
for (String str : strs) {
for (int j = 0; j < str.length (); j++)
cnt[j][str.charAt(j) - 'a']++;
}
StringBuilder res = new StringBuilder();
for(int i = 0;i < strs[0].length();i++){
if(cnt[i][strs[0].charAt(i) - 'a'] != strs.length )
break;
res.append (strs[0].charAt(i));
}
return res.toString();
}
}
上述的方法相当于把整个字符串数组从左到右,从上到下依次扫描了一遍,有些没必要的遍历仍然跑了一遍,所以造成了一定的冗余。
能不能优化一下呢?
我们可以通过不断求strs[i]和strs[i + 1]的公共前缀,然后依次向后判断来加速,从而避免了后面的无谓的判断,虽然算法时间复杂度并没有变化,都是,但是因为少了一些无谓的地方的统计,可以使程序的执行效率进一步提升。
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
String pre = strs[0];
for(int i = 1;i < strs.length ;i++)
while (strs[i].indexOf(pre) != 0)
pre = pre.substring(0, pre.length () - 1);
return pre;
}
}
首先,默认strs[0]为公共前缀,然后依次将其缩短(截取掉最后一个字符),不断求解与strs[i]的公共前缀。最终就可以得到所有strs[i]的公共前缀。
总结
虽然我们最开始提出的算法就能实现我们的目的,但是通过去除冗余的一些统计,我们仍然能找到可以优化的地方。所以不断摈弃落后的,冗余的,不仅仅是自然界的物竞天择,也是我们设计更快算法的核心理念。
力扣链接:https://leetcode.cn/problems/longest-common-prefix/
014. 最长公共前缀
题意
编写一个方法来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。
难度
简单
示例
输入:strs = ["flower","flow","flight"]
输出:"fl"
分析
这题并不难,只需要统计strs[i][j](字符串数组中每个字符串的每个字符)在j位置出现的次数即可,如果它出现了strs.length 次(字符串数组的长度),而且它的前缀也出现了strs.length ,那么它就是整个字符串数组的公共前缀。
所以我们只需要遍历这个字符数组,并且记录其相应的位置即可。
class Solution{
private int[][] cnt = new int[210][30];
public String longestCommonPrefix(String[] strs) {
for (String str : strs) {
for (int j = 0; j < str.length (); j++)
cnt[j][str.charAt(j) - 'a']++;
}
StringBuilder res = new StringBuilder();
for(int i = 0;i < strs[0].length();i++){
if(cnt[i][strs[0].charAt(i) - 'a'] != strs.length )
break;
res.append (strs[0].charAt(i));
}
return res.toString();
}
}
上述的方法相当于把整个字符串数组从左到右,从上到下依次扫描了一遍,有些没必要的遍历仍然跑了一遍,所以造成了一定的冗余。
能不能优化一下呢?
我们可以通过不断求strs[i]和strs[i + 1]的公共前缀,然后依次向后判断来加速,从而避免了后面的无谓的判断,虽然算法时间复杂度并没有变化,都是,但是因为少了一些无谓的地方的统计,可以使程序的执行效率进一步提升。
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
String pre = strs[0];
for(int i = 1;i < strs.length ;i++)
while (strs[i].indexOf(pre) != 0)
pre = pre.substring(0, pre.length () - 1);
return pre;
}
}
首先,默认strs[0]为公共前缀,然后依次将其缩短(截取掉最后一个字符),不断求解与strs[i]的公共前缀。最终就可以得到所有strs[i]的公共前缀。
总结
虽然我们最开始提出的算法就能实现我们的目的,但是通过去除冗余的一些统计,我们仍然能找到可以优化的地方。所以不断摈弃落后的,冗余的,不仅仅是自然界的物竞天择,也是我们设计更快算法的核心理念。
力扣链接:https://leetcode.cn/problems/longest-common-prefix/
知识星球
扫码加入星球
查看更多优质内容
https://wx.zsxq.com/mweb/views/joingroup/join_group.html?group_id=15522885221412