Topic

  • String

Description

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

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: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

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

Constraints:

  • 0 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lower-case English letters.

Analysis

Submission

public class LongestCommonPrefix {// 方法一:我写的public String longestCommonPrefix1(String[] strs) {if (strs == null || strs.length == 0)return "";StringBuilder sb = new StringBuilder();out: for (int i = 0; i < strs[0].length(); i++) {char current = strs[0].charAt(i);for (int j = 1; j < strs.length; j++) {if (strs[j].length() == i || strs[j].charAt(i) != current)break out;}sb.append(current);}return sb.toString();}// 方法二:public String longestCommonPrefix2(String[] strs) {if (strs == null || strs.length == 0)return "";for (int i = 0; i < strs[0].length(); i++) {char current = strs[0].charAt(i);for (int j = 1; j < strs.length; j++) {if (strs[j].length() == i || strs[j].charAt(i) != current)return strs[0].substring(0, i);}}return strs[0];}// 方法三:public String longestCommonPrefix3(String[] strs) {if (strs == null)return null;if (strs.length == 0)return "";String theShortest = strs[0], theLongest = strs[0];for (String str : strs) {if (str.compareTo(theShortest) < 0)theShortest = str;if (str.compareTo(theLongest) > 0)theLongest = str;}int i = 0;while (i < theShortest.length() && theShortest.charAt(i) == theLongest.charAt(i))i++;return theShortest.substring(0, i);
}

Test

import static org.junit.Assert.*;
import org.junit.Test;public class LongestCommonPrefixTest {@Testpublic void test() {LongestCommonPrefix obj = new LongestCommonPrefix();final String[] array1 = {"flower", "flow", "flight"};assertEquals("fl", obj.longestCommonPrefix1(array1));assertEquals("fl", obj.longestCommonPrefix2(array1));assertEquals("fl", obj.longestCommonPrefix3(array1));final String[] array2 = {"dog", "racecar", "car"};assertEquals("", obj.longestCommonPrefix1(array2));assertEquals("", obj.longestCommonPrefix2(array2));assertEquals("", obj.longestCommonPrefix3(array2));}
}

LeetCode - Easy - 14. Longest Common Prefix相关推荐

  1. 【easy!】LeetCode 14. Longest Common Prefix

    LeetCode 14. Longest Common Prefix Solution1: 用的暴力遍历,时间复杂度O(n2)O(n2)O(n^2) class Solution { public:s ...

  2. LeetCode算法入门- Longest Common Prefix -day13

    LeetCode算法入门- Longest Common Prefix -day13 题目描述: Write a function to find the longest common prefix ...

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  4. leetcode python3 简单题14. Longest Common Prefix

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...

  5. LeetCode:14. Longest Common Prefix

    两年硕士超快的鸭,又要准备秋招啦!0508第一题~ 题目 Write a function to find the longest common prefix string amongst an ar ...

  6. Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  7. LeetCode: 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 大意就是,写一个函数可以找 ...

  8. 【LeetCode】15.Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 从字符数组的第一项往后挨个 ...

  9. LeetCode in Python-14. Longest Common Prefix 最长公共前缀

    Longest Common Prefix 最长公共前缀 题目 解法1.逐位比较 解法2.利用集合性质 解法3.zip+sets 解法4.借助os模块自带函数... 出处 题目 解法1.逐位比较 cl ...

最新文章

  1. 又是找 Bug 的一天! | 每日趣闻
  2. Jake Coco - Under The Covers, Vol. 2 {2011}
  3. JDK 14 里的调试神器了解一下?
  4. f12控制台如何查看consul_Consul初探-从安装到运行
  5. diff和patch工具使用(转)
  6. ajax高效分页查询,基于 Jquery+Ajax+Json高效分页实现代码
  7. c#怎么设置一个字符串中某个字符的颜色
  8. UVA11152 Safe Salutations【计算几何】
  9. Java_银行排队叫号系统
  10. Alex 的 Hadoop 菜鸟教程: 第19课 华丽的控制台 HUE 安装以及使用教程
  11. 【毕业设计源码】基于SSM的小程序任务调度管理信息系统设计与实现
  12. 微信公众号开发-自定义菜单
  13. 计算机可以关闭家庭组,win10系统禁用(关闭)家庭组功能的具体方法
  14. 正方教务系统换数据库服务器,附:正方教务系统成绩录入操作说明- 正方教务系统成绩管理使用说明.doc...
  15. 乐山计算机学校有几个,乐山计算机学校全国排名是多少,好不好?
  16. Matlab中矩阵卷积函数convn
  17. 西安电子科技大学计算机科学与技术排名,西安电子科技大学双一流学科名单2个...
  18. 渗透测试--3.1.社会工程学攻击
  19. win7安装office2010
  20. 将mysql某一列形成多列

热门文章

  1. Makefile.am
  2. android studio adb 命令行,Android Studio如何配置adb以及经常使用命令
  3. 【转】wifi的几种工作模式
  4. 【转】VTK + QT + VS 编译安装配置
  5. 【转】AI-900认证考试攻略
  6. 2.1Dynamics 365 安装问题——无法访MSCRM_CONFIG数据库
  7. Web Reference和Service Reference的区别
  8. 第七节: EF的三种事务的应用场景和各自注意的问题(SaveChanges、DBContextTransaction、TransactionScope)
  9. angular设置referer_Angular-cli 构建应用的一些配置
  10. 【PAT - 甲级1010】Radix (25分)(二分,进制转化)