Leetcode_14_Longest Common Prefix
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/40555783
Longest Common Prefix
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 "";
String min = strs[0];
if (min.length() == 0)
return "";
if (strs.length == 1)
return min;
for (int i = 1; i < strs.length; i++) {
if (min.length() > strs[i].length())
min = strs[i];
}
StringBuffer buff = new StringBuffer();
boolean flag = false;
for (int i = 0; i < min.length(); i++) {
char c = min.charAt(i);
for (int j = 0; j < strs.length; j++) {
if (strs[j].length() != 0) {
if (strs[j].charAt(i) == c) {
flag = true;
continue;
} else {
flag = false;
return buff.toString();
}
}
}
if (flag) {
buff.append(c);
}
}
return buff.toString();
}
网上公认较好的解题算法如下所示:
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0)
return "";
int size = strs.length;
int j = 0;
int minlength = strs[0].length();
// find the min length of strings
for (String s : strs) {
if (s.length() < minlength) {
minlength = s.length();
}
}
// take substrings, put into a HashSet. if HashSet size >1, reduce the
// lengh of substrings;
while (j < minlength) {
HashSet<String> h = new HashSet<String>();
for (int i = 0; i < size; i++) {
h.add(strs[i].substring(0, minlength - j));
if (h.size() > 1)
break;
}
if (h.size() == 1)
return strs[0].substring(0, minlength - j);
j++;
}
return "";
}
Leetcode_14_Longest Common Prefix的更多相关文章
- [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 ...
- No.014:Longest Common Prefix
问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
随机推荐
- struts2 可以用ognl拿到值而不可以用el拿到值的解决方法
错误debug后 得到了There is no read method for container的错误 于是我new了一个实体类 package com.unity; public class St ...
- Linux 管理软件
公司的openfire先前运行在windows上的,但由于在windows上openfire内存机制问题,最多只能占用2GB内存,且时间稍微长久一些就会自动挂掉,用户无法登陆和连接,因此迁移到了Cen ...
- Jupyter Notebook
Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...
- 介绍Docker仓库
仓库(Repository)是集中存放镜像的地方. 一个容易混淆的概念是注册服务器(Registry).实际上注册服务器是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像.从 ...
- Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息
原文地址:http://www.javatang.com 前一段时间上线的系统升级之后,出现了严重的高CPU的问题,于是开始了一系列的优化处理之中,现在将这个过程做成一个系列的文章. 基本概念 在对J ...
- markdown绘图插件----mermaid简介
作者:黄永刚 mermaid简介 当撰写文档的时候,对于流程图的生成大多使用Visio等繁重的工具,没有一种轻便的工具能够画图从而简化文档的编写,就像markdown那样. mermaid解决这个痛点 ...
- Linux中的高级文本处理命令,cut命令,sed命令,awk命令
1.2.1 cut命令 cut命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields ## 用于有特定分隔字符 [r ...
- PHP Ajax JavaScript Json 实现天气信息获取
使用第三方服务 间接方式 思路 使用到的服务 实现代码 前端完整代码 总结 要在自己的网站上添加一个天气预报功能,是一个很普通的需求,实现起来也不是很难.今天来介绍几个简单的方法. 使用第三方服务 有 ...
- property干嘛的
>>> import datetime >>> class c(): @property def noww(self): return datetime.datet ...
- PHP学习(2)——运行环境搭建
学习PHP首先要搞定PHP的运行环境.PHP的运行环境包括:PHP语言解析器本身以及Apache服务器.MySQL数据库等.因为只是学习嘛,尽快的搭建起来运行环境就好,到后期慢慢懂得多了再去想规范化搭 ...