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. swift 基本用法

    Swift 也提供恒等(===)和不恒等(!==)这两个比较符来判断两个对象是否引用同一个对象实例. 判断字符串相等: let name = "world" if name == ...

  2. [Jmeter] Concurrency Thread Group

    Concurrency Thread Group : https://jmeter-plugins.org/wiki/ConcurrencyThreadGroup/ 参数介绍: Target Conc ...

  3. 效率类APP原型制作分享----Timeglass

    本原型由国产Mockplus(原型工具)和iDoc(智能标注,一键切图工具)提供. 主要页面:启动页面.主页.添加事件页面.设置页面等. mp文件下载:点击这里 在线预览:http://run.moc ...

  4. spring colud 博客

    https://blog.csdn.net/forezp/article/details/70148833

  5. linux 查看信息-服务器相关

    查看系统内核 查看磁盘信息 查看CPU的信息 查看内存相关信息

  6. rowspan和colspan的区别粗解

    rowspan和colspan是我们初学HTML表格中会在做一些特殊表格中遇到.其常在td中添加. rowspan的作用是指定纵向所跨越单元格的行数. 如下效果. colspan的作用是指定单元格横向 ...

  7. jrebel

    jrebel 编辑   JRebel是一套JavaEE开发工具.JRebel允许开发团队在有限的时间内完成更多的任务修正更多的问题,发布更高质量的软件产品. JRebel是收费软件,用户可以在JReb ...

  8. [ASP.NET]使用Oracle.ManagedDataAccess的OracleParameter参数化和OracleDataAdapter模糊查询

    今天写个查询员工的信息的demo遇到了2个问题 问题1.使用Oracle.ManagedDataAccess的OracleParameter参数化 OracleParameter 的使用(参数名要以: ...

  9. js 分页

    html代码:  <div id="paging_wrap" class="paging-wrap"></div> css代码: div ...

  10. 团队-Python 爬取豆瓣电影top250-成员简介及分工

    姓名:周鑫 班级:软件6班 团队名称:咣咣踹电脑 擅长:Python,java 分工:编写数据库