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. angular 滚动

    AngularJs $anchorScroll.$controller.$document $anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到 ...

  2. Linux (centos )下Nginx+PHP+MySQL配置——自己的lnmp配置

    说明:所有软件都是从官网上下载最新版的stable版本 ##################### 获取最新源码包###################### #建立独立的webserver#mkdi ...

  3. Unicode字符集下CString与char *转换 (解决中文乱码等)(转)

    UniCode 下 CString 转 char* 的方法的文章有很多,但是大部分都是在互相转载,看了那么多资料,仍然没有解决乱码的问题,后来从一个论坛的一条回复里面找到了正确的方法,特此拿出来与大家 ...

  4. Photoshop的评价

    Photoshop是Adobe公司旗下最为出名的图像处理软件之一. Photoshop的功能性:主要处理以像素所构成的数字图象.使用其众多的编修与绘图工具,可以有效地进行图片编辑工作.支持Window ...

  5. 一周学会go语言并应用 by王奇疏

    <一周学会go语言并应用> by王奇疏 ( 欢迎加入go语言群: 218160862 , 群内有实践) 点击加入 零.安装go语言,配置环境及IDE 这部分内容不多,请参考我的这篇安装环境 ...

  6. Android中ExpandableListView的使用

    ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView < L ...

  7. 创建Maven web项目时 出现 web.xml is missing and <failOnMissingWebXml> is set to true错误 pox.xml编译错误

    今天创建一个maven项目 pom.xml出现如下错误: web.xml is missing and <failOnMissingWebXml> is set to true 这是因为你 ...

  8. 自定义ANDROID中EDITTEXT中的HINT文本的大小

    EditText editText = (EditText) rootView.findViewById(R.id.et); // 新建一个可以添加属性的文本对象 SpannableString ss ...

  9. C#交错数组的用法

    class Program { static void Main(string[] args) { /* *交错数组,就是一个一维数组里面的每一项都是一个一维数组 *new交错数组的时候需要注意int ...

  10. Swift笔记

    最近从Xcode6 beta4开始到现在的Xcode6.0.1,使用Swift一段时间了,Swift大体来说,语法与java.c++比较接近,相比objective-c要友好多了,也更容易上手,这里记 ...