【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题。
题目描述:
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串
""
。示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母
a-z
。
总共有 5 种思路:
- 所求的最长公共前缀子串一定是每个字符串的前缀子串,所以随便选择一个字符串作为标准,把它的前缀子串与其它所有字符串进行判断,看是否是它们所有字符串的前缀子串。
- 列出所有的字符串的前缀子串,将它们合并后排序,找出其中个数为 n 且最长的子串。
- 纵向扫描,从下标 0 开始,逐一判断每个字符串的下标是否相等,直到遇见不全相同的下标位置为止。
- 横向扫描:前两个字符串找公共子串,将其结果和第三条字符串同样找公共子串,直到最后一个字符串为止。
- 借助 trie 字典树,将这些字符串存储到 trie 树中,那么 trie 树的第一个分叉口之前的单分支树的就是所求。
这里我使用纵向扫描。
解题代码:
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)return "";
if(strs.length == 1)return strs[0];
StringBuffer res = new StringBuffer();
StringBuffer shortly = new StringBuffer(strs[0]);
int shortlyIndex = 0;
for(int i=1; i<strs.length; i++) {
if(strs[i].equals(""))return "";
if(shortly.length() > strs[i].length()) {
shortly.delete(0, shortly.length());
shortly.append(strs[i]);
shortlyIndex = i;
}
}
strs[shortlyIndex] = strs[0];
strs[0] = shortly.toString();
//System.out.println(shortly);
for(int i=0; i<shortly.length(); i++) {
for(int j=1; j<strs.length; j++) {
if(strs[0].charAt(i) != strs[j].charAt(i)) {
return res.toString();
}
if(j == strs.length - 1) {
res.append(strs[0].charAt(i));
}
}
}
return res.toString();
}
}
提交结果:
个人总结:
题目有坑!strs = [] 的情况万万没想到。而且我这部分代码也是可以简化的,我这样写就是为了避免一些下标的越界错误。但其实没多大的必要。
【LeetCode】Longest Common Prefix(最长公共前缀)的更多相关文章
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- # 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. If there is n ...
- 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]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Longest Common Prefix -最长公共前缀
问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- db2数据库备份
一.离线备份 db2 list database directory -----查看有哪些数据库,确定需要备份哪个数据库 db2 disconnect current -----断开以数据库 ...
- 【Troubleshooting Case】Exchange Server 组件状态应用排错?
在Exchange 2013中,引入了“服务器组件状态”的概念.服务器组件状态从运行环境的角度提供对组成Exchange Server的组件的状态的精细控制. 日常排错时,常常会把Exchange 服 ...
- LR脚本示例之URL请求(post、get)
Action(){ //application/x-www-form-urlencoded //application/json //web_add_auto_header("Content ...
- MySQL基础环境_安装配置教程(Windows7 64或Centos7.2 64、MySQL5.7)
MySQL基础环境_安装配置教程(Windows7 64或Centos7.2 64.MySQL5.7) 安装包版本 1) VMawre-workstation版本包 地址: https://m ...
- 对比java和python对比
对比java和python 对比java和python 2011年04月18日 1.难易度而言.python远远简单于java. 2.开发速度.Python远优于java 3.运行速度.java远优于 ...
- JavaScript空假值及其判断
一.javaScript五种空值和假值 分别为undefined,null,false,"",0,这五个值的共同点是在执行if语句时都会执行false分支,执行对应的非语句的时候都 ...
- ajax请求执行完成后再执行其他操作(jQuery.page.js插件使用为例)
就我们做知,ajax强大之处在于它的异步请求,但是有时候我们需要ajax执行彻底完成之后再执行其他函数或操作 这个时候往往我们用到ajax的回调函数,但是假如你不想或者不能把接下来的操作写在回调函数中 ...
- HDU 3709 Balanced Number (数位DP)
题意: 找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数. 思路: 一开始以为需要枚举位数,枚举前缀和,枚举后缀和,一旦枚举起来就会M ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- Connectivity
6492: Connectivity 时间限制: 1 Sec 内存限制: 128 MB提交: 118 解决: 28[提交][状态][讨论版][命题人:admin] 题目描述 There are N ...