我现在在做一个叫《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. Google Tango Java SDK开发:Configure and Connect 配置和连接

    Configure and Connect 配置和连接 Note: This section assumes you are familiar with the Android Activity Li ...

  2. java程序无法启动:Unsupported major.minor version 51.0

    今天在sae上部署了一个项目,结果总是出现503错误:service unavailable,然后jvm出现了一大串错误日志,如下 JAVA_SAE_Fatal_error: Failed start ...

  3. cesium编程入门(八)设置材质

    cesium编程入门(八)设置材质 Cesium中为几何形状设置材质有多种方法 第一种方法 Material 直接构建Cesium.Material对象,通过设置Material的属性来进行控制,官方 ...

  4. winform app.cpnfig 文件的引用

    1.app.config配置文件修改 <?xml version="1.0" encoding="utf-8"?> <configuratio ...

  5. VisualStudio2017 远程 调试 IIS 服务器 web网站

    小伙伴们,本次测试好好的程序发布到服务器挂到IIS后我勒个*,,, 神马情况,为啥和我本地运行结果不一致,Fuc*... 没遇到的小伙伴估计也看不到此篇文章了吧,Log日志调试,嗯 不错,good i ...

  6. html中事件调用JavaScript函数时有return与没有return的区别

    JAVASCRIPT在事件中调用函数时用return返回值实际上是对window.event.returnvalue进行设置. 而该值决定了当前操作是否继续.当返回的是true时,将继续操作.当返回是 ...

  7. HBase – 存储文件HFile结构解析

    本文由  网易云发布. 作者:范欣欣 本篇文章仅限内部分享,如需转载,请联系网易获取授权. HFile是HBase存储数据的文件组织形式,参考BigTable的SSTable和Hadoop的TFile ...

  8. SQL Server IF Exists 判断数据库对象是否存在的用法

    1 判断数据库是否存在Sql代码 if exists (select * from sys.databases where name = ’数据库名’)    drop database [数据库名] ...

  9. 关于IPv4设置一些常见问题的解答

      问:为什么要填IP地址.网关.DNS? 如果是单部机拨号上网,是不需要填这些的.不填,或乱填,对上网都没有任何影响.但是在局域网上网(用路由器就属此环境),就一定要正确设置(手工填写或接受自动分配 ...

  10. CTF中密码学一些基础【三】

    本文作者:i春秋签约作家——MAX. 看看今天教程: 看着几个字符在键盘的位置,直接就是三个圈圈,圆心的三个字符就是答案 非常简单! 答案就是KEY 看题解密就好了!! 根据提示Asp encode解 ...