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. pyqt5

    使用python爬虫需要使用webkit执行javascript,查找资料可使用pyqt5,在此记录下安装步骤 windows 安装pyqt5 操作webkit 可用版本 1. 下载python 3. ...

  2. 硬件初始化,nand flash固化操作,系统启动简单流程

    2015.3.27星期五 晴 链接脚本定义代码的排放顺序 硬件系统初始化:一:arm核初始化:(里面有指令)初始化ARM核的时候需要看arm核的手册指令:1.异常向量(最起码有个复位异常,初始化模式- ...

  3. 【LeetCode】Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  4. Python 学习记录----利用Python绘制奥运五环

    import turtle #导入turtle模块 turtle.color("blue") #定义颜色 turtle.penup() #penup和pendown()设置画笔抬起 ...

  5. B样条基函数(cubic spline basis)

    B样条基函数用作权重 reference http://blog.csdn.net/tuqu

  6. 关于jQuery外部框架

    (function(window, undefined) {         var jQuery = ...         ...             window.jQuery = wind ...

  7. LaTeX_fleqn参数时,多行公式对齐居中的同时选择性的加编号

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/20 一年多没写博文了.今天写一个短的,记录一下使用LaTeX的一些经验. 有些时候,我们的latex文 ...

  8. java虚拟机之引用

    强引用: 类似:object A=new Object();这样的引用,只要强引用还存在,垃圾回收期就永远不会回收被引用的对象,eg:这里的new Oject().   软引用: 一些还有用,但是非必 ...

  9. oracle查询一个时间段每天的数据量

    1.需求: 从所有数据中,查出一个时间段中每天的数据量,即:按日做汇总. 2.SQL语句模板: select trunc(date_col) date, sum(num_col) num, count ...

  10. PHP的学习--PHP加密

    PHP中的加密方式有如下几种 1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str  --  原始字符串. ...