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 ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
随机推荐
- tar包和jar包和war包的区别?
tar:tar是*nix下的打包工具,生成的包通常也用tar作为扩展名,其实tar只是负责打包,不一定有压缩,事实上可以压缩,也可以不压缩,通常你看到xxxx.tar.gz,就表示这个tar包是压缩的 ...
- vue-router实现登录和跳转到指定页面,vue-router 传参
定义路由的时候可以配置 meta 字段: const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, childr ...
- Hibernate QBC 条件查询(Criteria Queries) and Demos
目录 创建一个Criteria 实例 限制结果集内容 结果集排序 关联 动态关联抓取 查询示例 投影Projections聚合aggregation和分组grouping 离线detached查询和子 ...
- Linux部分常用命令整理
./ 相当于双击 [oracle@linux01 ~]$ PWD 查看绝对路径 [oracle@linux01 ~]$ cd - 返回上一次操作的目录 [oracle@linux01 ~]$ cd . ...
- Linux 查看CPU温度
安装 lm-sensors sudo apt-get install lm-sensors # 安装yes | sudo sensors-detect # 侦测所有感测器 sensors # 查看温度 ...
- COS对象存储服务的使用
---------------------------------------------------------------------------------------------[版权申明:本 ...
- Lucene查询结果高亮
检索结果高亮 实现效果: 核心代码 package ucas.ir.lucene; import java.io.File; import java.io.IOException; import ja ...
- Python实现数据库一键导出为Excel表格
依赖 Python2711 xlwt MySQLdb 数据库相关 连接 获取字段信息 获取数据 Excel基础 workbook sheet 案例 封装 封装之后 测试结果 总结 数据库数据导出为ex ...
- Spring之AOP模块
AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个重要特征 Spring推荐使用接口编程 Spring提供三种拦截器:方法前拦截器.返回后拦截器.异 ...
- 合成/聚合复用原则(CARP)
组合/聚合复用原则(Composite/Aggregate Reuse Principle或CARP),就是在一个新的对象里面使用一些已有的对象,使之成为新对象的一部分,新对象通过向这些对象的委派达到 ...