1 题目:

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

Hide Tags

String

2 思路
 
所有字符串公共的前缀,那么第一个字符串肯定包括了。 从第一个字符串开始遍历着手即可。
 
3 代码:
    public String longestCommonPrefix(String[] strs) {
if(strs.length == 0) return "";
String string = strs[0];
int len = strs.length;
while(string.length()>0){
boolean isContain = true;
for(int i = 1; i < len; i++){
if(strs[i].startsWith(string)){ }else{
if(string.length() > 1)
string = string.substring(0,string.length()-1);
else{
return "";
}
isContain = false;
break;
}
}
if(isContain) return string;
}
return "";
}

[leetcode 14]Longest Common Prfix的更多相关文章

  1. Leetcode 14. Longest Common Prefix(水)

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

  2. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  4. [LeetCode] 14. Longest Common Prefix

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

  5. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  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. If there is n ...

  8. [LeetCode] 14. Longest Common Prefix ☆

    Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...

  9. [LeetCode]14. Longest Common Prefix最长公共前缀

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

随机推荐

  1. Lazarus下面的javascript绑定另外一个版本bug修正

    Lazarus下面的javascript绑定另外一个版本bug修正 从svn 检出的代码有几个问题 1.fpcjs.pas 单元开始有 {$IFDEF FPC} {$MODE delphi} {$EN ...

  2. [Hbase]Hbase章1 Hbase框架及基本概念

    Hbase框架介绍 HBase是一个分布式的.面向列的开源数据库. 不同点: l  和一般的关系数据库不同,hbase是一个适合于非结构化数据存储的数据库. l  Hbase是基于列而不是基于行的模式 ...

  3. import this

    import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than ...

  4. python中从内部循环直接跳出多层循环

    学习循环的时候碰到一道题,需要从内部循环中直接跳出所有循环,想了很久终于想到一种好办法(小白认知) 题目为:使用while循环输出100-50,从大到小,到50时,再循环输出0-50,从小到大. ex ...

  5. /bin/sh^M:bad interpreter:

    /bin/sh^M:bad interpreter: No such file or directory 这个错误发生在你在windows下编写文件上传到linux服务器去运行的时候. 错误原因:wi ...

  6. linux代码笔记

    sudo passwd root更新root密码 软件包管理及shell命令_deb软件包管理一_笔记:dpkj -i 安装dpkj -r 移除dpkj -P 全部移除dpkj -L 列出安装清单dj ...

  7. centos7 hdfs yarn spark 搭建笔记

    1.搭建3台虚拟机 2.建立账户及信任关系 3.安装java wget jdk-xxx rpm -i jdk-xxx 4.添加环境变量(全部) export JAVA_HOME=/usr/java/j ...

  8. oracle undo表空间

    查询undo表空间状态 "Bytes(M)" FROM dba_undo_extents GROUP BY tablespace_name, status; Undo表空间的状态( ...

  9. windows中当你的键盘无法使用时我们可以用另一种方法哦

    1.使用Win+R打开cmd窗口 2.输入osk回车就出现了一个虚拟的小键盘啦,当你的键盘坏掉后非常实用哦

  10. idea环境下js、css中文乱码

    idea2018.2+tomcat8+java8+win10 异常:本地js和css通过tomcat发布时,在页面打印出的中文是乱码.而从数据库读取的中文数据和html的中文显示正常. 解决方法: 步 ...