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. pycharm创建新django app

    Tools -> Run manage.py task -> startapp appName(你的App名称) 或者 optin + R  -> startapp appName( ...

  2. Servlet API

    Servlet API的查询网址:通过Tomcat的官网链接找到 可见,Servlet API有4个packages javax.servlet // 包含定义Servlet和Servlet容器之间契 ...

  3. java 检查异常 和 非检查异常

    个人见解 ,如果有问题 ,还希望大神们 指正 1. 非检查异常 又称运行时 异常 ,所有 继承自 RuntimeException 的异常都是 非检查异常  ,, 如果你不处理  会有 虚拟机 mai ...

  4. Java SE学习【三】——JDBC

    最近学到了数据库与java的jdbc方面,还有个DAO模式,写一下自己的理解,后期有什么不对的再改. 一.数据库三范式的理解 记得以前上课时,也上了一学期的“数据库系统原理”,给我们上课的老师算是渣渣 ...

  5. PHP代码不应有的坏习惯

    >>使用echo取代print >>使用str_replace取代preg_replace, 除非你绝对需要 >>不要使用 short tag >>简单 ...

  6. ios 内存管理与property copy strong weak assign

    - (void)fun{  NSString* str = [[NSString alloc] initWithString:@"string"];  NSLog(@"% ...

  7. 1-10假期训练(hdu-2059 简单dp)

    题目一:传送门 思路:水题,模拟即可 题目二:传送门 思路:dp,决策每个充电站是否要充电.(决策只有搜索,DP两种解决方法) (1)考虑状态的个数,n+2个,因为除了n个还有位置0,终点len两种状 ...

  8. WPF中的路由事件(转)

    出处:https://www.cnblogs.com/JerryWang1991/archive/2013/03/29/2981103.html 最近因为工作需要学习WPF方面的知识,因为以前只关注的 ...

  9. boost-数据类型之auto、any、tuple、variant

    1.auto.decltype   auto是C++11中的关键字,它可以通过类型推导自动得到变量或对象的类型,需要注意的是auto会忽略引用,因为引用其实就代表原对象: #include <v ...

  10. 修正剑桥模型预测-用python3.4

    下面是预测结果: #!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" ""& ...