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

 public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
} else if (strs.length < 2) {
return strs[0];
} String r = strs[0];
int index = r.length();
for (int i = 1; i < strs.length; i++) {
int j = 0;
for (; j < strs[i].length() && j < index; j++) {
if (strs[i].charAt(j) != r.charAt(j)) {
break;
}
}
if (j == 0) {
return "";
}
index = j;
} return r.substring(0, index);
}
}

[LeetCode] 14. Longest Common Prefix的更多相关文章

  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. Java [leetcode 14] Longest Common Prefix

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

  5. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

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

  7. [LeetCode] 14. Longest Common Prefix ☆

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

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

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

  9. LeetCode——14. Longest Common Prefix

    一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...

随机推荐

  1. 每天学习一点点--word-break和word-wrap用法和区别

    有时候一个又臭又长的单词出现在一个并不宽到足以容纳这个单词时会出现内容溢出容器这种情况: <!DOCTYPE html> <html lang="en"> ...

  2. 好用的开源爬虫 jsoup

    中文Api http://www.open-open.com/jsoup/ 英文Api https://jsoup.org/

  3. Map循环的三种方法

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class MapTest { pu ...

  4. Linux上安装oracle客户端及sqlldr

    1.建立oracle的安装目录,一般为/opt/oracle:2.将安装包instantclient-basic-linux.x64-11.2.0.3.0.zip放入到该目录中,并进行解压:unzip ...

  5. selenium win7+selenium2.0+python环境搭建

  6. LayaAir引擎——(四)

    LayaAir引擎 TiledMap 使用 所需要的软件: Tiled地图编辑器 版本0.16.2 LayaAir IDE 所需要的图片:图块图片(1.png) 步骤1: 文件->新文件-> ...

  7. 模拟jQuery库

    用js模拟jQuery方法,体会封装思想 <!DOCTYPE html><html><head><meta charset="UTF-8" ...

  8. Angular--$http服务

    关于Angular中$http 服务是对XMLHttpRequest 对象的封装,向服务器发送请求: 下面自己在angular中$http学习的一个记录, GET请求(先贴码) angualr代码: ...

  9. Oracle expdp按分区导出生成参数文件

    使用dba_tab_partitions视图获得每个分区的参数文件内容,使用chr(10)分行 select 'content=data_only'||chr(10)|| 'directory=dat ...

  10. HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...