Problem: 找出给定的string数组中最长公共前缀
 
由于是找前缀,因此调用indexOf函数应当返回0(如果该字符子串为字符串的前缀时),如果不是则返回-1
Return:
the index of the first occurrence of the specified substring, or -1 if there is no such occurrence.
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 最长公共前缀
*/
public class Solution14 {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == ) return "";//字符串数组为空或者长度为0
String pre = strs[];
int i = ;
while(i < strs.length){//遍历所有字符串
while(strs[i].indexOf(pre) != )//当前子串不满足前缀
pre = pre.substring(,pre.length()-);//当前子串长度减一
i++;
}
return pre;//返回前缀
}
}

LeetCode 14 Longest Common Prefix(最长公共前缀)的更多相关文章

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

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

  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 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  4. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

  5. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  6. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  7. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

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

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  9. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  10. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

随机推荐

  1. MarkDown 使用说明示例

    一.标题 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 一级标题 这是 H2 这是 H3 一级和二级标题还有一种写法 就是下面加横杆,同时 超过2个的 = 和 - 都可以有效果. Thi ...

  2. 安装drools workbench

    从drools官网下载tomcat7版本的Drools  Tomcat 7+ WAR, Workbench,实际就是一个war包,需要严格按照里面的readme的要求,配置好tomcat才可以运行起来 ...

  3. PHP获取MySql新增记录ID值的3种方法

    From: http://www.jb51.net/article/51473.htm 这篇文章主要介绍了PHP获取MySql新增记录ID值的3种方法,一般使用PHP自带函数mysql_insert_ ...

  4. TensoFlow的tf.reshape()

    tf.reshape(tensor,shape,name=None) 函数的作用是将tensor变换为参数shape形式,其中的shape为一个列表形式,特殊的是列表可以实现逆序的遍历,即list(- ...

  5. utf8 ucs4

    这个问题不好回答,首先UTF-8编码只不过是一种Unicode的转换,兼容ASCII.所以,UTF-8编码支持的最大字符编码应该是Unicode支持的最大字符编码. 理论上,UTF-8编码可以支持最大 ...

  6. asp.net gridview实现正在加载效果方案一AJAX(转)

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...

  7. UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程

    lienhua342014-11-08 在进程控制三部曲中我们学习了进程的创建.终止以及获取终止状态等的进程控制原语.线程的控制与进程的控制有相似之处,在表 1中我们列出了进程和线程相对应的控制原语. ...

  8. VCL 中的 Windows API 函数(1): AbortDoc

    AbortDoc: 该函数终止当前打印作业并删除最好一次调用 StartDoc 函数写入的所有信息. 该函数在 Printers 单元的应用:AbortDoc(Canvas.Handle);

  9. JAXB基本使用

    JAXB主要用来实现对象和XML之间的序列化和反序列化,关于JAXB的介绍就不多说了,网上一搜一大把,这里主要总结下基本使用方法和一些注意事项 首先定义两个示例类ClassA,ClassB,用于后续的 ...

  10. Vim终极指南:所思即所得

    https://blog.csdn.net/dc_726/article/details/78981756