Longest Common Prefix 解答
Question
Write a function to find the longest common prefix string amongst an array of strings.
Solution
第一思路是用Trie,但是对于一道Easy类别的题目,用Trie显然是杀鸡用牛刀。
于是我们可以参考到构造Trie的过程,来找这个最长prefix。
我们先用String array中第一个String初始化Pre,然后遍历剩下的每一个String,找重合的终点,删除不重合的部分。
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) {
return "";
}
int length = strs.length;
int initLen = strs[0].length();
StringBuilder result = new StringBuilder(initLen);
for (int i = 0; i < initLen; i++) {
result.append(strs[0].charAt(i));
}
for (int i = 1; i < length; i++) {
String curString = strs[i];
initLen = result.length();
for (int j = 0; j < initLen; j++) {
if (j < curString.length() && curString.charAt(j) == result.charAt(j)) {
continue;
}
result.delete(j, initLen);
break;
}
}
return result.toString();
}
}
更简单的方法是用String.indexOf()的方法来判断重合终点。
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) {
return "";
}
int length = strs.length;
int initLen = strs[0].length();
String prev = strs[0];
for (int i = 0; i < length; i++) {
String curString = strs[i];
while (curString.indexOf(prev) != 0) {
prev = prev.substring(0, prev.length() - 1);
}
}
return prev;
}
}
Longest Common Prefix 解答的更多相关文章
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- Longest Common Prefix -最长公共前缀
问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
随机推荐
- puppet证书重申
- maven install 报错Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of ...
- oracle日期计算
查询某月有多少天.代码例如以下: select to_number(add_months( trunc(to_date('2014-11-4 11:13:53','yyyy-mm-dd hh24:mi ...
- VMware vSphere 5.5的12个更新亮点(3)
端口镜像 有时有必要捕捉网络中的数据包来追踪问题.最新版本的vSphere包括一个增强版的开源数据包分析器tcpdump和一些镜像端口的选项以捕捉各种场所的流量.您可以捕获虚拟网卡,虚拟交换机,以及主 ...
- 【精品】Android游戏类源码大集合
Android自定义效果——随机抽奖 一个模拟抽奖的效果,用户设定若干个选项,添加之后,就可以通过程序,来帮助随机选择其中一项出来.这个类似超市里面那种指针转盘抽奖,run之后是一个动态效果图,初始快 ...
- Geodatabase - 打开要素类
string dbPath = @"G:\doc\gis\1.400\data\pdb.mdb"; ESRI.ArcGIS.Geodatabase.IWorkspaceFactor ...
- window.dialogArguments的使用
<HTML> <HEAD> <TITLE>showModelessDialogEX.htm</TITLE> <SCRIPT> var sUs ...
- c#快捷键设置和text输入限制
快捷键 使用KeyDonw事件 输入限制使用 KeyPress 事件 1.注意:如果是整个窗体的快捷键,一定要把窗体属性中的KeyPreview改为true private void textbox_ ...
- web多语言的一种处理方式
在开发一种国际化系统时,多语言是必须的. 总的来说处理方式有两种,一种是后端处理,另一种是前端处理.呵呵,有点废话~~ 后端处理没用过,猜猜是在标记需要处理语言的地方进行替换. 前端处理是要先把语言文 ...
- log在线生成器 html中如何设置浏览器中标题前的logo
制作logo的地址:http://www.logomaker.com.cn/ 设置网站logo的方法 在<head></head>标签之间输入<link rel=&quo ...