【LeetCode】 Longest Common Prefix
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
题目的意图
It seems that it is not to check between pair of strings but on all the strings in the array.
For example:
{"a","a","b"} should give "" as there is nothing common in all the 3 strings.
{"a", "a"} should give "a" as a is longest common prefix in all the strings.
{"abca", "abc"} as abc
{"ac", "ac", "a", "a"} as a.
Logic goes something like this:
Pick a character at i=0th location and compare it with the character at that location in every string.
If anyone doesn't have that just return ""
Else append that character in to the result.
Increment i and do steps 1-3 till the length of that string.
return result.
Make sure proper checks are maintained to avoid index out of bounds error.
代码
public class Solution {
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == )
return "";
String pre = strs[];
int i = ;
while (i < strs.length) {
System.out.println(strs[i].indexOf(pre) );
while (strs[i].indexOf(pre) != )
pre = pre.substring(, pre.length() - );
i++;
}
return pre;
}
}
【LeetCode】 Longest Common Prefix的更多相关文章
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- 【Leetcode-easy】Longest Common Prefix
思路:每次从字符数组中读取两个字符串比较.需要注意输入字符串为空,等细节. public String longestCommonPrefix(String[] strs) { if(strs==nu ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- 【SPOJ】Longest Common Substring
[SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- 用HAWQ轻松取代传统数据仓库(八) —— 大表分区
一.HAWQ中的分区表 与大多数关系数据库一样,HAWQ也支持分区表.这里所说的分区表是指HAWQ的内部分区表,外部分区表在后面“外部数据”篇讨论.在数据仓库应用中,事 实表通常有非常多 ...
- STL容器共性机制和使用场景
一.STL容器共性机制 STL容器所提供的都是值(value)寓意,而非引用(reference)寓意,也就是说当我们给容器中插入元素的时候,容器内部实施了拷贝动作,将我们要插入的元素再另行拷贝一份放 ...
- 在C#中实现截获shell程序的输出
在Windows环境下的所谓shell程序就是dos命令行程序,比如VC的CL.exe命令行编译器,JDK的javac编译器,启动java程序用的java.exe都是标准的shell程序.截获一个sh ...
- RCE、exp、Exploit、Exploit Pack、exp-gui、Payload、MetaSploit都是啥
对于走在安全路上的小菜来说,这几个exp.Exploit.Exploit Pack.exp-gui.Payload.MetaSploit名词着实把人转的不轻,下面给大家解释下: RCE,remote ...
- qq图片选择效果的处理
QQ中图片鼠标一选择,整个图片就像加了个阴影一样,这个效果一般人都不会注意,突然没事测试了一下,原来qq是把原来每个像素的颜色变成了相反的颜色. 电脑中的三原色为0-255,中间值为128,以中间值为 ...
- web 应用 及 补充
Highcharts 绘图配置 的函数及参数 web页面文本框修饰器 --- KindEditor web页面 之 超人性的点赞与狂踩 web页面 之 图片上传 web页面 之 评论盖楼 jQuery ...
- 2015.12.14 MDI(多文档窗口结构)设置基本解决,折腾一天,部分解决存在已久的问题。但效果仍不如临时航线的MDI窗体结构。
创建从一个窗口弹出多个子窗口的结构叫MDI窗体结构 如果不按MDI结构管理,最简单的做法是: 在窗体A上添加菜单或按钮,在菜单或按钮事件中添加弹出B窗体代码: B b = new B(); b.sho ...
- php中的foreach改变数组的值的问题
翻到PHP文档的foreach那页这样写道: “foreach 语法结构提供了遍历数组的简单方式.foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误 ...
- python(时间模块,序列化模块等)
一.time模块 表示时间的三种方式: 时间戳:数字(计算机能认识的) 时间字符串:t='2012-12-12' 结构化时间:time.struct_time(tm_year=2017, tm_mon ...
- 获取Linux权限后安装rootkit
1.首先获得远程服务器的root权限,当然这是基本的也是最难的. 2.然后下载rootkit程序,本文用到的是mafix. 3.开始安装 wget http://godpock.googlecode. ...