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 ...
随机推荐
- 介绍几个移动web app开发框架
jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mobile应用用户界面.jQuery Mobile最新版本是1.4.0,默认主题采用扁平化设计风格.j ...
- perl 学习笔记
一:基础 1:安装perl centos: yum -y install perl 官网:https://www.perl.org/ 升级到5.22:先下载,执行./i ...
- Activity小结
Log日志类的五种级别 1.由高到低分别是:v.i.d.w.e 2.生命周期有七种状态: onCreate:创建 onStart:启动 onResume:显示(可以与用户交互) onPause:暂停 ...
- swift 随机数
1.一行代码生成随机数 arc4random() 如果要生成一个生成在一定范围内的随机整数: func randomIn(#min: Int, max: Int) -> Int { retur ...
- iphone真机开发流程之--证书申请
一.申请 1.进入ios开发者中心 http://www.apple.com.cn/developer/ios/index.html 2.点击登录 输入用户名和密码(前提:已经有Apple ID,且花 ...
- C++——try、throw、catch实例学习程序
#include<iostream> #include<stdexcept> //exception/stdexcept/new/type_info头文件里都有定义的标准异常类 ...
- 解决linux不能使用chmod更改权限的问题
本人安装的是win10和ubuntu的双系统,发现在ubuntu下挂载windows硬盘不用命令chmod更改文件的权限,解决方法记录如下: 对于使用命令$ chmod 777 dirname更改不了 ...
- NSString属性什么时候用copy,什么时候用strong?【转】
转自:http://www.cocoachina.com/ios/20150512/11805.html. 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境): ...
- webform开发经验(一):Asp.Net获取Checkbox选中的值
webform中获取repeat控件列表下的checkbox选中的值: 码农上代码: public static string getSelectedIDs(Repeater Rpt_) { stri ...
- DOM事件处理程序-事件对象-键盘事件
事件流: 事件流--描述的是从页面中接受事件的顺序 IE ---事件冒泡流:即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至最不具体的那个节点(文档). Netsc ...