我现在在做一个叫《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. python int函数转换浮点型字符串的坑???

    python中的int函数可以将数字或字符串转换为整型数字类型,具体功能就不提了 最近发现一个问题,对于字符串'1.1'之类的,int转换的时候会报异常,这是为什么,个人感觉直接转换成1不就行了,干嘛 ...

  2. Understanding String Table Size in HotSpot

    In JDK-6962930[2], it requested that string table size be configurable.  The resolved date of that b ...

  3. HRBUST1311 火影忍者之~忍者村 2017-03-06 16:06 106人阅读 评论(0) 收藏

    火影忍者之-忍者村   忍者村是忍者聚居的村子,相等于国家的军事力量.绝大部分村民都是忍者,有一些忍者会在村内开设书店.餐厅等,不过大部分忍者都是为村子执行任务的忍者,以赚取酬劳,并于战时为国家出战. ...

  4. 企业搜索引擎开发之连接器connector(二十六)

    连接器通过监视器对象DocumentSnapshotRepositoryMonitor从上文提到的仓库对象SnapshotRepository(数据库仓库为DBSnapshotRepository)中 ...

  5. handsontable-developer guide-cell function

    renderer 展示的数据不是来自于数据源,而是先把DOM和其他信息传给renderer,然后展示. //五种展示函数 TextRenderer: default NumericRenderer A ...

  6. android多lib库工程的自动批量构建--准备

    打包这个活儿吧,本来没什么技术含量,千篇一律的更改渠道名称,然后编译,签名即可.但是如果发布渠道比较多的话,这种重复工作让人烦不胜烦.我们的应用发布渠道有30多个,若是手工打包,基本不用做其他的事情了 ...

  7. [label][PHP-Security]PHP Security Program

    本文是通过阅读http://www.nowamagic.net/中的PHP安全变成专题,同时结合个人的一点点开发经验组合而成的. 如果你需要看原文,可以直接访问http://www.nowamagic ...

  8. Postgresql 用户管理

    一, 设置超级用户密码 1  修改 pg_hba.conf 使超级用户postgres 可以登录到数据库中 host all all 127.0.0.1/32 trust 2 修改 postgres ...

  9. 知物由学 | AI网络安全实战:生成对抗网络

    本文由  网易云发布. “知物由学”是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不会知道.“知物由学” ...

  10. iOS 需要了解的

    1. 用 CocoaPods 来管理应用用到的第三方库. 2. Jenkins 3. Robotium 4. RESS