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. jsp页面错误的全局处理

    网上搜索spring mvc项目全局异常处理: 大致可以找到两种方案 : 方案1: ExceptionHandlerResolver . spring 提供了两种默认实现,当然你也可以自己实现.. 方 ...

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

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

  3. 软件工程网络15个人作业4--Alpha阶段个人总结

    一.个人总结 在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 请用自我评价表:http://www.cnblogs.com/xinz/p/3852177.html 有 ...

  4. HQL和SQL查询

     转自http://blog.csdn.net/aaa1117a8w5s6d/article/details/7757097 HQL和SQL的区别 标签: sqlhibernatejavasessio ...

  5. python学习 day4 (3月5日)---列表

    列表: 容器性数据 有序  可更改 大量数据 一.增 1.追加    append(objcet) 2.索引增加   Insert(index,元素) 3.迭代追加   extend(object) ...

  6. 52.tableViewCell重用机制避免重复显示问题

    表刷新超出页面显示的内容会重复出现 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInd ...

  7. De Bruijn序列

    最近文章中经常出现及De Bruijin 这个关键字,网上搜索了一下,记录下来. De Bruijn序列 (德布鲁因序列) 问题:能否构造一个长度为2的n次方的二进制环状串,使得二进制环状串中总共2的 ...

  8. hdu 1540(线段树区间合并)

    题目链接:传送门 参考文章:传送门 题意:n个数字初始连在一条线上,有三种操作, D x表示x号被摧毁: R 表示恢复剩下的通路 Q表示查询标号为x所在的串的最长长度. 思路:线段树的区间合并. #i ...

  9. hibernate执行createSQLQuery时字段重名的问题

    hibernate执行createSQLQuery时字段重名的问题 不同表关联 ,表字段重名 =>之前若 as 别名 会自动区分 但有一次签移到新服务器  mysql 5.5上: 若字段重名:重 ...

  10. s4-7 生成树协议

    Spanning Tree :为了可靠,采用冗余结构:但是透明网桥 会产生无休止循环的问题 冗余交换拓扑可能带来的问题  广播风暴  多帧传送  MAC地址库不稳定 生成树协议  STP:sp ...