【算法】LeetCode算法题-Longest Common Prefix
这是悦乐书的第146次更新,第148篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串,如果没有则返回空串。其中,字符串数组中的元素都是由小写字母a-z之间随机组合而成。例如:
输入:["flower","flow","flight"]
输出:"fl"
输入: ["dog","racecar","car"]
输出: ""
输入:["c"]
输出:"c"
本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。
02 第一种解法
第一步:获取数组的第一个元素first。
第二步:截取first字符串的0-1位,判断数组从第二个元素到最后一个元素是否都能匹配到截取的字符串,匹配到count就加1,如果count最后的值和数组除掉第一个元素后的长度相等,则是共有前缀。
第三步:如果第二步成功匹配上,则截取first字符串的0-2位,重复第二步的判断逻辑。
public static String longestCommonPrefix(String[] strs) {
String result = "";
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0];
}
String first = strs[0];
for (int i=1; i<=first.length(); i++) {
String prefix = first.substring(0, i);
int count = 0;
for (int j=1; j<strs.length; j++) {
if (strs[j].indexOf(prefix) == 0) {
count = count + 1 ;
}
}
if (count != 0 && count == strs.length-1) {
result = prefix;
}
}
return result;
}
03 第二种解法
第一步:获取数组中第一个元素first。
第二步:用first和数组第二个元素匹配查找,找不到就循环将first元素从0到倒数第二位截取,直到first变为空,如果first为空则表示没有相同的前缀。
第三步:用first和数组第二个元素的共有前缀与数组第三个元素进行匹配查找,依次往后循环。
public static String longestCommonPrefix2(String[] strs) {
if (strs.length == 0) {
return "";
}
String first = strs[0];
for (int i=1; i<strs.length; i++) {
while (strs[i].indexOf(first) != 0) {
first = first.substring(0, first.length()-1);
if (first.isEmpty()) {
return "";
}
}
}
return first;
}
04 第三种解法
先将原数组分为两部分,左边部分依次获取共有前缀,右边部分依次获取共有前缀,再将左右两边的前缀进行查找,最后得到所有元素共有的前缀。此方法有点绕,可以通过调试或做标记及的方式理解。
public String longestCommonPrefix3(String[] strs) {
if (strs.length == 0) {
return "";
}
return partOf(strs, 0, strs.length-1);
}
public String partOf(String[] strs, int leftIndex, int rightIndex) {
if (leftIndex == rightIndex) {
return strs[leftIndex];
} else {
int midIndex = (leftIndex + rightIndex)/2;
String leftStr = partOf(strs, leftIndex, midIndex);
String rightStr = partOf(strs, midIndex+1, rightIndex);
return getResult(leftStr, rightStr);
}
}
public String getResult (String leftStr, String rightStr) {
int min = Math.min(leftStr.length(), rightStr.length());
for (int i=0; i<min; i++) {
if (leftStr.charAt(i) != rightStr.charAt(i)) {
return leftStr.substring(0, i);
}
}
return leftStr.substring(0, min);
}
05 小结
今天这题要解出来不难,难的是这是否是当前的唯一解?是否还可以另寻他法?
如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

本文首发于我的个人公众号:悦乐书,转载请注明出处!
【算法】LeetCode算法题-Longest Common Prefix的更多相关文章
- 乘风破浪:LeetCode真题_014_Longest Common Prefix
乘风破浪:LeetCode真题_014_Longest Common Prefix 一.前言 如何输出最长的共同前缀呢,在给定的字符串中,我们可以通过笨办法去遍历,直到其中某一个字符不相等了,这样就得 ...
- Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- leetcode第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)
1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
随机推荐
- maven创建一个简单的web项目
1.确认maven插件和配置在eclipse中已经完成 如果没完成,可参考这篇博客:http://www.cnblogs.com/mmzs/p/8191979.html 2.在eclipse中用mav ...
- javascript小实例,编写一个方法,实现从n-m个数中随机选出一个整数
别怪我是一个闷葫芦,没那么多花哨的语言,废话不多说,先说说小实例的要求: 编写一个方法,实现从n-m个数中随机选出一个整数,要求:传递的参数不足两个或者不是有效数字,返回[0-1]之间的随机数,需要解 ...
- [转] javascript 保留两位小数 (且不四舍五入)
本文转自:https://blog.csdn.net/qq_40171039/article/details/79729503 保留两位小数且不四舍五入: 方法一: var a = 2.461; va ...
- .net 公共基础类
using WL.Infrastructure.Http; using System; using System.Collections.Generic; using System.IO; using ...
- c# 画布验证码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 腾讯防水墙(滑动验证码)的简单使用 https://007.qq.com
在线体验:https://007.qq.com/online.html 快速开始:https://007.qq.com/quick-start.html 简单使用: 1. 引入 JS <scri ...
- App测试流程及测试点(个人整理版)
1 APP测试基本流程 1.1流程图 1.2测试周期测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间.正式测试前先向主 ...
- nginx配置虚拟机
在/usr/local/nginx/conf目录下nginx.conf文件是nginx的配置文件. 一.通过端口号区分虚拟机 在nginx.conf文件中添加一个Service节点,修改端口号: se ...
- IP是什么 DNS 域名与IP有什么不同
IP地址是在网络上分配给每台计算机或网络设备的32位数字标识.在Internet上,每台计算机或网络设备的IP地址是全世界唯一的.IP地址的格式是 xxx.xxx.xxx.xxx,其中xxx是 0 到 ...
- redirection in linux
2>&1 # Redirects stderr to stdout. # Error messages get sent to same place as standard output ...