题目:

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

思路:求最长前缀子串,假设第一个字符串是最长前缀子串,采用增强for得到数组中每个字符串,分别与第一个字符串的同一位置进行比较,有一个字符串在该位置不一致,就返回。

public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length==0){
return "";
} for(int i=0;i<strs[0].length();i++){//设定第一个字符串是最长前缀子串,挨个与其他字符串同一位置进行比较
for(String str:strs){
if(i==str.length()||str.charAt(i)!=strs[0].charAt(i))//发现有不同的就返回
return strs[0].substring(0,i);
}
}
return strs[0];
}
}

  

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

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

  4. Leetcode 14. Longest Common Prefix(水)

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

  5. 【LeetCode】14. Longest Common Prefix 最长公共前缀

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

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

  7. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  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. 解法: 广度优先搜索:先比 ...

随机推荐

  1. Redis介绍及实践分享

    1.Redis是什么 1)Redis是REmote DIctionary Server的缩写,是一个key-value存储系统 2)Redis提供了一些丰富的数据结构,包括Strings,Lists, ...

  2. 【转】JVM 分代GC策略分析

    我们以Sun HotSpot VM来进行分析,首先应该知道,如果我们没有指定任何GC策略的时候,JVM默认使用的GC策略.Java虚拟机是按照分代的方式来回收垃圾空间,我们应该知道,垃圾回收主要是针对 ...

  3. CentOS下用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境

    安装 系统环境:CentOS 6.5 安装依赖 yum -y install gcc gcc-c++ make git patch openssl-devel zlib-devel readline- ...

  4. android学习笔记42——图形图像处理2——绘图

    绘图 android的绘图应该继承View组件,并重写onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Can ...

  5. LDAP过滤器使用说明(用户、组和容器的默认 LDAP 过滤器和属性)

    说明来源:http://docs.oracle.com/html/E35191_01/ldap-filters-attrs-users.html#ldap-filters-attributes-use ...

  6. 【MySQL】数据行长度的一些限制

    今天开发在导入数据的时候报一个错误: Row size too large. The maximum row size for the used table type, not counting BL ...

  7. (C/C++) memset

    C语言: memset   extern void *memset(void *buffer,int c,int count);   #include <string.h>   功能:把b ...

  8. MSSQL学习笔记

    阅读目录 1.什么是SQL语句 2.使用sql语句创建数据库和表 3.创建数据表 4.数据完整性约束 5.四中基本字符类型说明 6.SQL基本语句 7.类型转换函数 8.日期函数 9.数学函数 10. ...

  9. BEvent_客制化BusinessEvent通过Workflow Event接受消息传递(案例)

    2014-08-03 Created By BaoXinjian

  10. 为什么学习html5

    html5  2010年正式推出 新的web时代 优势: 1,跨平台运行                      PC MAC LINUX 手机 PAD 2,硬件要求低 3,flash之外的选择 h ...