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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

思路:

根据第一个字符串,不断去循环判断后面的字符串

代码:

class Solution {
public String longestCommonPrefix(String[] strs) {
if(null == strs || strs.length == 0){
return "";
}else if(strs.length == 1){
return strs[0];
}
String result = "";
for(int i = 1;i<=strs[0].length();i++){
String prefix = strs[0].substring(0,i);
for(int j=1;j<strs.length;j++){
if(strs[j].startsWith(prefix)){
if(j==strs.length-1){
result = prefix;
}
}else{
break;
}
}
}
return result;
}
}

String.substring(0,x),第二个参数很神奇,x就算超过字符串的长度也没关系,所以不用担心数组越界问题

【LeetCode算法-14】Longest Common Prefix的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  3. C# 写 LeetCode easy #14 Longest Common Prefix

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

  4. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

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

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

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

  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. Solution: cla ...

  9. 【LeetCode】14. Longest Common Prefix (2 solutions)

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

  10. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. swift 学习- 15 -- 构造过程 01

    // 构造过程 是使用类,结构体 或 枚举类型的实例之前的准备过程, // 在新实例可用前必须执行这个过程, 具体操作包括 设置实例中每个存储型属性的初始值 和 执行其他必须的设置 或 初始化工作 / ...

  2. Confluence 6 缓存性能优化

    Confluence 的运行状态与缓存状态有这密切的关系.针对 Confluence 的管理员来说,尤其是大型站点的 Confluence 管理员,设置好缓存尤其显得关键. 希望修改缓存的大小: 进入 ...

  3. dubbo源码之Directory与LoadBalance

    Directory: 集群目录服务Directory, 代表多个Invoker, 可以看成List<Invoker>,它的值可能是动态变化的比如注册中心推送变更.集群选择调用服务时通过目录 ...

  4. C++ Primer 笔记——基本内置类型

    1.算术类型分为两类:整型和浮点型.算术类型的尺寸在不同机器上有所差别,下表列出了C++标准规定的尺寸的最小值.同时允许编译器赋予这些类型更大的尺寸. 一个char的大小和一个机器字节一样. 一个in ...

  5. spring cloud 路由网关zuul的高可用

    Zuul的高可用非常关键,因为外部请求到后端微服务的流量都会经过Zuul.故而在生产环境中,我们一般都需要部署高可用的Zuul以避免单点故障. 笔者分两种场景讨论Zuul的高可用. Zuul客户端也注 ...

  6. tensorflow:验证码的识别(下)

    上两篇详细的说明了验证码的识别,不过我们采用的是方法二,下面采用方法一.注意和方法二的区别. 验证码识别方法一: 把标签转为向量,向量长度为40.(4位数字验证码) 验证码的生成和tf.record的 ...

  7. 纯css3实现的switch开关按钮

    效果如图 <p> <label><input class="mui-switch mui-switch-anim" type="checkb ...

  8. 让simplejson支持datetime类型的序列化

    simplejson是Python的一个json包,但是觉得有点不爽,就是不能序列化datetime,稍作修改就可以了: 原文:http://blog.csdn.net/hong201/article ...

  9. C#动态系统托盘图标

    C#动态系统托盘图标 利用timer组件定时执行变化. using System; using System.Windows.Forms; namespace DynamicStockIcon { p ...

  10. 配置frp

    一.下载 下载地址:https://github.com/fatedier/frp/releases 下载linux_amd64的,如果是32位系统就下载linux_386 二.安装 有公网IP的服务 ...