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的更多相关文章

  1. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  2. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  3. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  4. 【Leetcode-easy】Longest Common Prefix

    思路:每次从字符数组中读取两个字符串比较.需要注意输入字符串为空,等细节. public String longestCommonPrefix(String[] strs) { if(strs==nu ...

  5. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  6. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  7. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  8. 【SPOJ】Longest Common Substring

    [SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. LeetCode 480. Sliding Window Median

    原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...

  2. hdu 1028 && hdu 1398 && hdu 1085 && hdu 1171 ——生成函数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1028 就是可以用任意个1.2.3....,所以式子写出来就是这样:(1+x+x^2+...)(1+x^2+ ...

  3. 聊聊WPF中字体的设置

    1. 今天帮同事调试一个字体的bug:TextBox中的中文显示大小不一致, 比如包含"杰","热". 原因是WPF针对点阵字体需要指定特定字体才能正确渲染, ...

  4. django1.7+nginx1.4.4的static配置

    静态文件指像css,js,images之类的文件. 1.工程配置setting.py STATIC_URL = /static/ STATIC_ROOT = /home/www/winingcpi/s ...

  5. Azure CLI的Query

    Azure CLI 2.0是基于Python的命令行.其命令直观,使用非常方便. 其输出有四种模式: --output -o : Output format. Allowed values: json ...

  6. Azure上通过haproxy实现APP Gateway或WAF的http跳转https

    Azure上的APP Gateway是七层负载均衡服务,WAF是APP Gateway服务的扩展.在实现七层负载均衡的同时,增加了WAF的功能,可以对后台的HTTP服务进行保护. Azure WAF采 ...

  7. hihoCoder1296:约瑟夫问题

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个圈,依次编号为0.. ...

  8. Spring学习五

    1: servlet生命周期:  Servlet加载    ->   实例化->   服务 ->  销毁 2:Servlet重要函数: init():在Servlet的生命周期中,仅 ...

  9. RabbitMQ 消息队列 应用

    安装参考    详细介绍   学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...

  10. mysql 回顾小练习

    Student(id,sname,age,sex) 学生表 Course(id,cname,t_id) 课程表 SC(s_id,c_id,score) 成绩表 Teacher(id,Tname) 教师 ...