我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

014.Longest Common Prefix[E]

问题

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

Subscribe to see which companies asked this question

思路

这个没啥思路的,怎么都要两重循环,因为是最长公共子串,随便找一个(一般是第一个作为基准),然后拿拿的首部慢慢去匹配后面的字符串就行了。

public class Solution {
public String longestCommonPrefix(String[] strs) {
String s = "";
if(strs.length == 0)
return "";
for(int i = 0; i < strs[0].length();i++)
{
char c = strs[0].charAt(i);
for(int j = 1;j < strs.length;j++)
{
if(i >= strs[j].length() || strs[j].charAt(i) != c)
return s;
}
s += c;
}
return s;
}
}

《LeetBook》leetcode题解(14):Longest Common Prefix[E]的更多相关文章

  1. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

  2. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  4. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  5. C# 写 LeetCode easy #14 Longest Common Prefix

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

  6. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  7. 【LeetCode】14 - Longest Common Prefix

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

  8. 【LeetCode】14. Longest Common Prefix (2 solutions)

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

  9. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

随机推荐

  1. android事件分发

    1). android对事件分发的顺序为:Activity--->PhoneWindow--->DecorView--->yourView; 2). android控件对事件处理的优 ...

  2. PHP爬虫(3)PHP DOM开源代码里的大坑和字符编码

    一.开源代码的问题 在PHP爬虫(2)中介绍了开源工程Sunra.PhpSimple.HtmlDomParser.在实际工作中发现一个问题,例如http://www.163.com的网页数据怎么也抓取 ...

  3. SSH案例--入门级

    1.项目功能展示 (1)注册 (2)修改地址与级别信息,点击修改   (3)再添加一位成员,进行删除 点击第二行的删除 (4)登录模块测试 输入数据库中没有的信息: 输入数据库中存在的信息: 2. W ...

  4. Solr 从文件创建索引

    http://blog.csdn.net/clj198606061111/article/details/21492457 http://wiki.apache.org/solr/Extracting ...

  5. elasticsearch不能使用root启动问题解决

    问题: es安装好之后,使用root启动会报错:can not run elasticsearch as root [root@iZbp1bb2egi7w0ueys548pZ bin]# ./elas ...

  6. ASP.NET Core学习总结(3)

    我们重点来看看这个InvokeInnerFilterAsync. protected override async Task InvokeInnerFilterAsync() { var next = ...

  7. cesium编程入门(九)实体 Entity

    cesium编程入门(九)实体 Entity 在cesium编程入门(五)绘制形状提到过添加实体的方法,这一节聊一聊实体相关的一些内容: 先来看 Entity 的各个属性 id 唯一标志,如果没设置, ...

  8. Android 色差(尤其白色)的解决办法

    Android 中有时出现色差,我碰到的情况是 Galaxy ACE4 中的白色和系统白色不同,所以显示时候颜色不同,很难看. 我发现的问题是 Color.white, android.R.color ...

  9. 1305 Pairwise Sum and Divide(数学 ,规律)

    HackerRank   1305 Pairwise Sum and Divide   有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = ...

  10. 中山纪念中学培训DAY1

    哇啊啊啊啊啊啊$……$ 并不像说环境怎么样. $Day1$模拟赛 稳重一点选了提高$B$ 然后$5min$后: $t1$装压$DP$最短路 $t2$裸地贪心 $t3……$哇$t3$怎么做啊啊啊啊. $ ...