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 ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
随机推荐
- Python自动化测试入门
在当前自动化测试中,最火的语言就是Python,很容易上手.然后就是Java+testng+appium做的UI自动化测试.下面我们就用Python脚本,做自动化集成测试. (1)获取APK文件中ID ...
- async/await 的一些知识
博文 Don't Block on Async Code What is the purpose of "return await" in C#? Any difference b ...
- sa账号无法登陆sqlserver2008
今天遇到sa无法登陆sqlserver2008的问题,原来是sa账户未启用,混合验证模式没打开,太低端了. 具体解决过程将从百度文库里查到的文章张贴如下: 出现问题 : 标题: 连接到服务器 ---- ...
- PyCharm 2018.1破解过程
一.下载 首先从官网下载 官网,如果开了酸酸乳的话无法下载,官网会自动断开连接.所以下载时请关闭酸酸乳 二.安装 选择安装路径 选择64位,创建关联.py文件 安装完后运行Pycharm 选择不导入开 ...
- JavaScript Window Navigator
window.navigator 对象包含有关访问者浏览器的信息. Window Navigator window.navigator 对象在编写时可不使用 window 这个前缀. 实例 <d ...
- Ruby方法参数默认值的一个小技巧在Rails中的应用
我们需要生成一个gravatar格式的html.image标示,于是写了如下方法: def gravatar_for(user) gravatar_id = Digest::MD5::hexdiges ...
- RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment
Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...
- 好用的SQLAlchemy
准备 安装SQLAlchemy框架 测试代码 知识点剖析 引入库支持 基类和引擎 实体类 声明类 数据库自动完成 CRUD 总结 这里简单的记录一下本人第一次使用SQLAlchemy这个ORM框架的过 ...
- Apache shiro集群实现 (七)分布式集群系统下---cache共享
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- 阻塞IO服务器模型之多线程服务器模型
针对单线程服务器模型的特点,我们可以对其进行改进,使之能对多个客户端同时进行响应.最简单的改进即是使用多线程(或多进程)服务器模型,在应用层级别,我们一般采用多线程模式.多线程能让多个客户端同时请求, ...