问题:

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

官方难度:

Easy

翻译:

寻找一个字符串数组的最长公共前缀。

方法一:

  1. 数组长度为0,返回空字符串。
  2. 将数组第一项,作为初始的公共前缀。
  3. 从数组第二项开始进入循换,整理当前字符和当前前缀字符的长度。
  4. 以小长度的字符串为基准,从头开始逐个匹配,遇到不同的字符时,更新前缀字符。
  5. 为防止完全匹配情况,如“aa”匹配“aaa”。由于不存在不同的字符,所以不会更新的情况,设定更新标志位flag,当flag为false时,将前缀字符更新为小长度字符。
  6. 在内循环结束之后,若得到的前缀字符长度为0,直接跳出整个循环。
  7. 外循环可以使用foreach循环。

方法一的解题代码:

     private static String method(String[] strs) {
String prefix = strs.length == 0 ? "" : strs[0];
// 从数组第二项开始遍历
for (String compare : strs) {
// 整理长度
String s1 = prefix.length() > compare.length() ? compare : prefix;
String s2 = prefix.length() > compare.length() ? prefix : compare;
boolean flag = false;
for (int j = 0; j < s1.length(); j++) {
if (s1.charAt(j) != s2.charAt(j)) {
prefix = prefix.substring(0, j);
flag = true;
break;
}
}
// 防止完全匹配的情况
if (!flag) {
prefix = s1;
}
if (prefix.length() == 0) {
break;
}
}
return prefix;
}

method

方法二:

  1. 其实,String类自带了String.startWith()的实例方法,来判断一个字符串是否以另一个字符串为开头的形式。使用这种方法可以进一步地增强效率。
  2. 在内循环中,判断当前字符是否匹配当前前缀,若不是消去前缀字符串的最后一个字符,直到匹配成功。
  3. 注意入参检查。

方法二的解题代码:

     public static String longestCommonPrefix(String[] strs) {
if (strs == null) {
throw new IllegalArgumentException("Input error");
}
String prefix = strs.length == 0 ? "" : strs[0];
for (String compare : strs) {
while (!compare.startsWith(prefix)) {
// 不匹配,消去前缀最后一项
prefix = prefix.substring(0, prefix.length() - 1);
}
if (prefix.length() == 0) {
break;
}
}
return prefix;
}

longestCommonPrefix

相关链接:

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

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q014.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.014:Longest Common Prefix的更多相关文章

  1. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode第[14]题(Java): Longest Common Prefix

    题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array o ...

  3. LeetCode OJ:Longest Common Prefix(最长公共前缀)

    Write a function to find the longest common prefix string amongst an array of strings. 求很多string的公共前 ...

  4. LeetCode 14: Longest Common Prefix

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

  5. 【LeetCode】LeetCode——第14题:Longest Common Prefix

    14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...

  6. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  7. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  8. No.014 Longest Common Prefix

    14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...

  9. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

随机推荐

  1. OutputCache属性详解(三)— VaryByHeader,VaryByCustom

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  2. 搭建LNAMP环境(五)- PHP7源码安装Redis和Redis拓展

    上一篇:搭建LNAMP环境(四)- 源码安装PHP7 一.安装Redis 1.创建redis用户组和用户 groupadd redis useradd -r -g redis -s /sbin/nol ...

  3. git查看日志

    git查看日志 git log -n 显示前N条记录 git log -3 退出log命令 直接输入: q git log --stat -n 显示提交的文件的简要的修改统计 $ git log -- ...

  4. React(二)实现双向数据流

    <div id="app"></div> <script src="bower_components/react/react.min.js& ...

  5. 实现步骤: 推送&传感器&UIDynamic

    一.本地通知基本使用: #01.请求授权(8.0以前默人授权) #02.创建本地通知 #03.设置通知内容 #04.设置通知时间(多久后发通知) #05.发送通知 二.本地通知而外设置: #01.设置 ...

  6. Javascript设计模式系列学习笔记

    因为是学习笔记,里面并没有很多注释和讲解,所有不太适合0基础的朋友看,只能说抱歉了. 这些笔记目前还存在很多的问题,不过我相信再今后的学习过程中会把这些问题挨个的解决. 除了前面3节后面的都不分前后顺 ...

  7. 原生JS实现分页效果1.0

    不太完整,写的太急,等等加上完整注释,写起来还是有些难度的,写的有点水,后面再改进改进. <!DOCTYPE html><html lang="en">&l ...

  8. gridview里日期显示格式

    Text='<%#Bind("EndDate","{0:yyyy-MM-dd}") %>'

  9. javascript类型系统——Math对象

    × 目录 [1]常量 [2]函数 前面的话 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是 ...

  10. CSS三列布局

    × 目录 两侧定宽中间自适应 两列定宽一侧自适应 中间定宽两侧自适应一侧定宽两列自适应三列自适应总结 前面的话 前面已经介绍过单列定宽单列自适应和两列自适应的两列布局.本文介绍三列布局,分为两侧定宽中 ...