题目:

  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. 从1970年1月1日00:00:00 GMT以来此时间对象表示的毫秒数转化为Datetime

    1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)对时间转换 将Long类型转换为DateTime类型 /// <summary> /// 将L ...

  2. [rootfs]Yaffs2

    1. busybox: sudo apt-get install busybox(v1.21.1) 2. mkyaffs2image: http://www.aleph1.co.uk/gitweb/? ...

  3. 返回顶部(解决IE6固定定位)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. nginx 负载均衡-- 常用nginx配置

    中文官方网站http://wiki.nginx.org/Chshttp://www.howtocn.org/ --------------------------------------------- ...

  5. Lnmp环境的自搭建

    ### 备选#### 安装开发者工具包 (简约版的可能要安装一下) yum groupinstall "Development tools" ########## 1.准备 php ...

  6. Hibernate4日志及配置文件

    1. 确定要使用日志的实现log4j 2. Slf4japi.jar和log4j的jar包放入classpath,(slf4j-log4j.jar) 3. 编写log4j.properties 4.  ...

  7. [Java Web – 3A] – Spring MVC开发注意事项

    1.使用Maven构建项目 2.SpringMVC 绝对路径的问题 首先要明确一点,在html中,资源文件也是有自己的URL,即href中是支持绝对路径.如下代码: <link href=&qu ...

  8. PLSQL_性能优化系列10_Oracle Array数据组优化

    2014-09-25 Created By BaoXinjian

  9. Educational Codeforces Round 15 Powers of Two

    Powers of Two 题意: 让求ai+aj=2的x次幂的数有几对,且i < j. 题解: 首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后 ...

  10. 图片_ _ Bitmap_Drawable_Image?

    ===== 2 ==== 1 b.读取res/drawable目录下的 png或者bmp Resources r = this.getContext().getResources(); //以数据流的 ...