题目链接:https://leetcode.com/problems/longest-common-prefix/

题目:Write a function to find the longest common prefix string amongst an array of strings.

解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀。依次与后面的元素进行比較。取其公共部分,比較结束后。剩下的就是该字符串数组的最长公共前缀。演示样例代码例如以下:

public class Solution
{
public String longestCommonPrefix(String[] strs)
{
if (strs == null || strs.length == 0)
{
return "";
}
String prefix = strs[0];
for (int i = 1; i < strs.length; i++)
{
int j = 0;
while (j < strs[i].length() && j < prefix.length() && strs[i].charAt(j) == prefix.charAt(j))
{
j++;
}
if (j == 0)
{
return "";
}
prefix = prefix.substring(0, j);
}
return prefix;
}
}

【LeetCode OJ 14】Longest Common Prefix的更多相关文章

  1. 【LeetCode算法-14】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  2. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  3. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

  4. 【牛客网】Longest Common Subsequence

    [牛客网]Longest Common Subsequence 发现只有d数组最格路 于是我们把前三个数组中相同的数记成一个三维坐标,同一个数坐标不会超过8个 从前往后枚举d,每次最多只会更新不超过8 ...

  5. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  6. 【leetcode】Longest Common Prefix

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

  7. 【leetcode】Longest Common Prefix (easy)

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

  8. 【LeetCode每天一题】Longest Common Prefix(最长前缀)

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  9. 【LeetCode】 Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

随机推荐

  1. ZBrush软件特性之Material

    在ZBrush中,任何物体表面的外观都是多种因素的综合结果,例如基础颜色.纹理图像投落到表面上的照明效果和材质属性.材质可以改变照明在表面上的反应,以便模型表现出光泽.凹凸.反射.金属性或透明效果.Z ...

  2. [Python随笔]>>range()函数?

    因为自己在考核的时候没有记清range()函数的具体用法,所以特意去查了下 Python range() 函数用法 python range() 函数可创建一个整数列表,一般用在 for 循环中 函数 ...

  3. iview中单击行,使得checkbox状态的方法

    直接贴代码,这是一组jquery全选,全不选,反选代码 <!DOCTYPE html> <html lang="en"> <head> < ...

  4. 紫书 习题 8-22 UVa 1622 (构造法)

    这道题的构造法真的复杂--要推一堆公式--这道题写了几天了--还是没写出来-- 一开始简单的觉得先左右来回, 然后上下来回, 然后把剩下的执行完了好了, 然后就WA. 然后换了个思路, 觉得是贪心, ...

  5. ajax异步刷新

    前台js <script type="text/javascript"> function getLands() { $.ajax({ url:"httpse ...

  6. windows 下面的grep awk 命令

    windows 下面的grep awk 命令 grep 学习了:http://blog.csdn.net/chengfans/article/details/53784936 awk学习了:http: ...

  7. UI组件之AdapterView及其子类(四)Gallery画廊控件使用

    听说 Gallery如今已经不使用了,API使用ViewPaper取代了,以后再学专研ViewPaper吧如今说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...

  8. systemverilog中堵塞和非堵塞事件以及同步

    一.SV中非堵塞事件 module test; event ev1, ev2; //belong to logic function part always@(ev1) $display(" ...

  9. 曲根英语万词---二、evoke

    曲根英语万词---二.evoke 一.总结 一句话总结:evoke v.唤起,引起 词根:-voc-, -vok- [词根含义]:声音,叫喊 1.consecrate? v,供奉,奉为神圣 -ate, ...

  10. CMS系统简介(从简介到使用)

    CMS系统简介 1.简介 CMS是Content Management System的缩写,意为"内容管理系统". 在中国互联网的发展历程中,一直以来默默地为中国站长提供动力的CM ...