题目:Write a function to find the longest common prefix string amongst an array of strings.

  很简单的一个描述,最长公共前缀,首先想到的是用递归,既然是找公共前缀,那肯定要两个进行比较,所以把第一个和除了第一个之外的字符串数组看作两个进行比较,再对后面的进行递归就好了,上代码。

public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0)
return "";
if (strs.length == 1)
return strs[0]; return help1(strs);
}
//对进入的字符串数组进行“分开” 操作
public static String help1(String[] strs) {
String[] newStrs = new String[strs.length - 1];
for (int i = 0; i < strs.length - 1; i++) {
newStrs[i] = strs[i + 1];
}
return help2(strs[0], newStrs);
}
//分开之后,进入help2,迭代“分开”。
public static String help2(String str, String[] strs) {
StringBuffer returnString = new StringBuffer();
String[] newStrs = new String[strs.length - 1];
if (str != "") {
if (strs.length == 1) {
// 比较 取公共前缀
for (int i = 0; i < str.length() && i < strs[0].length(); i++) {
if (str.charAt(i) != strs[0].charAt(i)) {
break;
}
returnString.append(str.charAt(i));
}
} else {
for (int i = 0; i < strs.length - 1; i++) {
newStrs[i] = strs[i + 1];
}
return help3(str,help2(strs[0], newStrs));
}
return returnString.toString();
}
return "";
} //对迭代返回的数据进行比较
public static String help3(String str1,String str2) {
StringBuffer returnString = new StringBuffer();
for (int i = 0; i < str1.length() && i < str2.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
break;
}
returnString.append(str1.charAt(i));
}
return returnString.toString();
}

  之后看了官方的solution,思路大体上是差不多的,但是实现很巧妙。先比较第一第二个,再取一二的公共前缀来比较第三个,不过这里比较的方式有点特殊。先看第二个字符串里面index(第一个字符串)是不是等于0,是的话就继续用第一个字串比较后面的(因为第一个字符串是第二个字符串的前缀了,index()为0),如果不为0,也就是说第一个字符串不是第二个的前缀,那么就把第一个字符串缩短一位,再比较,直到第一个字符串为空。上代码:

  

public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++)
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) return "";
}
return prefix;
}

Leetcode 14——Longest Common Prefix的更多相关文章

  1. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  2. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  4. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  5. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. [LeetCode] 14. Longest Common Prefix ☆

    Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...

  8. [LeetCode]14. Longest Common Prefix最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  9. LeetCode——14. Longest Common Prefix

    一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...

随机推荐

  1. TypeError: Error #1034: 强制转换类型失败:无法将 "0.49" 转换为 mx.graphics.IFill。

    1.错误描述 TypeError: Error #1034: 强制转换类型失败:无法将 "0.49" 转换为 mx.graphics.IFill. at mx.charts.ser ...

  2. Struts(五)Action的访问

    在struts开发中,Action作为框架的核心类,实现对用户的请求的处理,Action被称为业务逻辑控制器.一个Action类代表一次请求或调用.Action就是用来处理一次用户请求的对象 Acti ...

  3. Codeforces Round #432 Div. 1 C. Arpa and a game with Mojtaba

    首先容易想到,每种素数是独立的,相互sg就行了 对于一种素数来说,按照的朴素的mex没法做... 所以题解的简化就是数位化 多个数同时含有的满参数因子由于在博弈中一同变化的,让他们等于相当于,那么这样 ...

  4. 【原】【译文】理解storm拓扑并行度

    原文地址: http://storm.apache.org/releases/1.2.1/Understanding-the-parallelism-of-a-Storm-topology.html ...

  5. Python爬虫之模拟登录微信wechat

    不知何时,微信已经成为我们不可缺少的一部分了,我们的社交圈.关注的新闻或是公众号.还有个人信息或是隐私都被绑定在了一起.既然它这么重要,如果我们可以利用爬虫模拟登录,是不是就意味着我们可以获取这些信息 ...

  6. 【BZOJ3143】游走(高斯消元,数学期望)

    [BZOJ3143]游走(高斯消元,数学期望) 题面 BZOJ 题解 首先,概率不会直接算... 所以来一个逼近法算概率 这样就可以求出每一条边的概率 随着走的步数的增多,答案越接近 (我卡到\(50 ...

  7. [BZOJ1001] [Beijing2006] 狼抓兔子 (最短路)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...

  8. [BZOJ4198] [Noi2015] 荷马史诗 (贪心)

    Description 追逐影子的人,自己就是影子. ——荷马   Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是 ...

  9. [BZOJ1552] [Cerc2007] robotic sort (splay)

    Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. Output ...

  10. 如何在Win10下安装MySQL 5.7绿色版

    一.背景 系统升级到Win10后准备在本地搭建一个MySQL环境,用于研究学习.在网上找了很多其他人写的经验总结,Step by step的做,不断的遇到问题,没有成功. 最后老老实实的去读Mysql ...