【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. ...
随机推荐
- Navicat中MySQL server has gone away错误怎么办【转载】
转载链接:http://www.111cn.net/database/mysql/64073.htm mysql数据库出现MySQL server has gone away错误一般是sql语句太大导 ...
- Oracle中查询前10条记录
在Oracle怎样查询表中的top10条记录呢? select * from test where rownum <=10 ----说明:rownum只能用于<或<=运算,如 ...
- git之log
1 查看提交的具体文件 git log --oneline --stat 可参考: http://www.cnblogs.com/BeginMan/p/3577553.html
- 配置文件的继承与覆盖: Machine.config / Web.config /子目录 Web.config
C#有三种级别的配置文件: 机器级别 machine.config 在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.c ...
- Spring Boot发布和调用RESTful web service
Spring Boot可以非常简单的发布和调用RESTful web service,下面参考官方指导体验一下 1.首先访问 http://start.spring.io/ 生成Spring Boot ...
- 得到properties配置文件内容
代码: 1.配置文件内容 2.文件所在项目中位置: 3.java代码: 01.得到键值对: @Test public void getProp() { Properties prop = new Pr ...
- [Angularjs-学习笔记]工具篇
因为一开始学习前端知识一直都是在慕课网,所以这次准备学习下angularjs等了好久,终于慕课网出了angularjs的内容,于是准备开始跟着老师的步骤进行学习. 大漠老师关于开发工具的内容讲得比较快 ...
- iOS离屏渲染
为什么会使用离屏渲染 当使用圆角,阴影,遮罩的时候,图层属性的混合体被指定为在未预合成之前不能直接在屏幕中绘制,所以就需要屏幕外渲染被唤起. 屏幕外渲染并不意味着软件绘制,但是它意味着图层必须在被显示 ...
- Celery-4.1 用户指南: Concurrency (并发)
简介 Eventlet 的主页对它进行了描述:它是一个python的并发网络库,可以让你更改如何运行你的代码而不是怎么编写代码. 对高可扩展非阻塞IO操作,它使用 epoll或者libevent. C ...
- 2015.1.15 利用Oracle函数返回表结果 重大技术进步!
-- sql 调用 select * from table( get_airway_subpoint(x,x,x)) ///////////////////////////////////////// ...