题目:

  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桌面管理工具 RedisDesktopManager

    下载链接地址:[官网地址:https://redisdesktop.com] redis-desktop-manager-0.8.8.384.exe Source code (zip) Source ...

  2. BeX5平台简明部署过程

    http://wex5.com/cn/concise-deployment/ BeX5平台简明部署过程 该文章主要介绍BeX5平台开发完成后,资源部署至正式环境的过程. 一. 获取BeX5企业快速开发 ...

  3. 利用NTFS权限与虚拟目录,在IIS 6.0的默认FTP站点中做用户隔离。

    默认FTP站点为不隔离用户站点,利用NTFS权限设置,达到仅能访问指定目录效果. 是否允许匿名连接 FTP站点主目录:站点范围内有没有用户需要上传,有的话,要勾选“写入”:具体用户使用NTFS还给予写 ...

  4. 关于mac mini组装普液晶显示器

    申请了好久的mac mini,部门终于给买下来了.没想到,买回来之后的组装还是折腾了我们一把.  因为先前没用过mac mini,以为它和普通的台式机一样,买回来就能直接到显示器上用了.结果买回来ma ...

  5. jsch ssh服务器调用Linux命令或脚本的小问题

    代码如下: public static boolean execshell(String command, String user, String passwd, String host) throw ...

  6. ubuntu配置DNS

    众所周知,centos配置DNS很简单,修改下/etc/resolv.conf,就可以生效.但是ubuntu中,resolv.conf文件却说明写入会被覆盖,不能写在这里. 方法一: 修改/etc/n ...

  7. 树莓派通过apt方式安装opencv库

    1.安装opencv     开始之前进行必要的更新工作. sudo apt-get update     安装opencv. sudo apt-get install libcv-dev     安 ...

  8. CSS如何实现数字分页效果

    代码实例如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  9. Android学习笔记02

    1.线性布局LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  10. 4个理由告诉你Java为何排行第一

    Java已经有20年的历史了,甚至更久,而这取决于你所询问的人和你的计算方式.忽略它的年龄不看,Java依然排行第一.它的实用性.性能和向后兼容性都彰显其价值所在.2016年伊始,标志着我们已经走过了 ...