1. 原题链接

https://leetcode.com/problems/longest-common-prefix/description/

2. 题目要求

给定一个字符串数组,让你求出该数组中所有字符串的最大公共前缀。例如{"qqwwee", "qqww", "qqfds"}的最大公共前缀为"qq",{"qqwwee", "qqww", "qqfds", "wea"}的最大公共前缀为""(空)。

3. 解题思路

(1)首先判断该字符串数组长度是否为零,为零直接返回最大公共前缀为空;

(2)假设该字符串数组中第一个元素strs[0]为最大公共前缀,然后与第二个元素进行比较,判定第二个元素是否包含假定的最大公共前缀,且假定的最大公共前缀的起始位置为0。满足以上要求,则证明该元素与之前的元素共同拥有该前缀。如果不包含,则证明假定的公共前缀不符合要求,将假定的公共前缀缩短一位,然后重新比较。

4. 代码实现

 public class LongestCommenPrefix14 {

     public static void main(String[] args) {
String[] strs = {"weeer", "weer", "wer"};
System.out.println(LongestCommenPrefix14.longestCommonPrefix(strs));
} public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0)
return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) != 0) { //与假定的公共前缀不匹配
prefix = prefix.substring(0, prefix.length() - 1); //缩短假定的公共前缀
if (prefix.equals(""))
return "";
}
}
return prefix;
}
}

LeetCode:14. Longest Commen Prefix(Easy)的更多相关文章

  1. 「Leetcode」14. Longest Common Prefix(Java)

    分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...

  2. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  3. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  4. LeetCode:5. Longest Palindromic Substring(Medium)

    原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...

  5. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

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

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

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

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

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

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

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

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

随机推荐

  1. Fiori里花瓣的动画效果实现原理

    Fiori里的busy dialog有两种表现形式,一种是下图里的花朵形状,由5个不断旋转的花瓣组成.另一种是下图的3/4个圆环不断旋转的效果. 关于前者的效果,可以看我制作的这个视频.这个视频是手动 ...

  2. PIL 图像字符画绘制

    from PIL import Image ascii_char = list('"$%_&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]? ...

  3. 2018.11.30 zsh: command not found: mysql 的解决办法mac环境下

    系统环境变量的问题.解决办法: 1.打开终端.输入open .zshrc 会出现一个可编辑文档, 2.找到# User configuration部分,在下一行添加source ~/.bash_pro ...

  4. SignalR集成Autofac

    SignalR SignalR集成需要 Autofac.SignalR NuGet 包. SignalR 集成提供SignalR 集线器的依赖集成.由于 SignalR 是内部构件,所以不支持Sign ...

  5. 【luogu P3865 ST表】 模板

    跟忠诚是一样滴,不过是把min改成max就AC了.模板题. #include <cstdio> #include <algorithm> using namespace std ...

  6. view围绕圆心自转

    创建一个image UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )]; imgView.ima ...

  7. Hands-On Modeler (建模人员参与程序开发)

    如果编写代码的人员认为自己没必要对模型负责,或者不知道让模型为应用程序服务,那么这个模型就和程序没有任何关联.如果开发人员没有意识到改变代码就意味着改变模型,那么他们对程序的重构不但不会增强模型的作用 ...

  8. (eclipse)统一文件编码和代码风格

    前言 1>每个人的代码风格不一样,以至于代码各式各样,有习惯=号左右加空格的,有习惯不加的,此举有时还会影响svn提交代码 2>注释代码不一样,并且注释风格也不一样 统一文件编码和代码风格 ...

  9. bootstrap到底是用来做什么的

    Bootstrap官网:http://v3.bootcss.com/ Bootstrap是Twitter推出的一个用于前端开发的开源工具包.它由Twitter的设计师Mark Otto和Jacob T ...

  10. spring cloud 学习之路由网关(zuul)

    学习自方志朋的博客 http://blog.csdn.net/forezp/article/details/69939114 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费. ...