14. 最长公共前缀

14. Longest Common Prefix

题目描述

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

LeetCode14. Longest Common Prefix

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z。

Java 实现

  1. class Solution {
  2. public String longestCommonPrefix(String[] strs) {
  3. if (strs == null || strs.length == 0) {
  4. return "";
  5. }
  6. String pre = strs[0];
  7. int i = 1;
  8. while (i < strs.length) {
  9. while (strs[i].indexOf(pre) != 0) {
  10. pre = pre.substring(0, pre.length() - 1);
  11. }
  12. i++;
  13. }
  14. return pre;
  15. }
  16. }

参考资料

LeetCode 14. 最长公共前缀(Longest Common Prefix)的更多相关文章

  1. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

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

  2. Java实现 LeetCode 14 最长公共前缀

    14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...

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

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  4. [LeetCode]14.最长公共前缀(Java)

    原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...

  5. python(leetcode)-14最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  6. leetcode 14 最长公共前缀

    描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...

  7. LeetCode 14.最长公共前缀(Python3)

    题目: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow& ...

  8. 【Leetcode】【简单】【14最长公共前缀】【JavaScript】

    题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  9. LeetCode:最长公共前缀【14】

    LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...

随机推荐

  1. 安装vs code之后,win+e快捷键打开的是vs code,而不是文件管理器,解决方法

    安装vs code之后,win+e快捷键打开的是vs code,而不是文件管理器,解决方法 xdg-mime default dde-file-manager.desktop inode/direct ...

  2. 《挑战30天C++入门极限》C++中类的多态与虚函数的使用

        C++中类的多态与虚函数的使用 类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持 ...

  3. mac clion c/c++环境配置

    下载安装:https://www.cnblogs.com/sea-stream/p/11220036.html 切换语言:https://www.cnblogs.com/sea-stream/p/11 ...

  4. hadoop错误记录部分总结

    错误记录与分析 错误1:java.net.BindException: Port in use: localhost:0 datanode节点启动时报错 日志信息如下: Exiting with st ...

  5. 两个对象key相同但是value不同,将value不同的键值对以对象形式输出

    let obj={ name:'jack', age:18, sex:'girl' } let obj2={ name:'rose', age:18, sex:'boy' } var str={} f ...

  6. 采用正则表达式实现startWith、endWith效果函数

    startsWith函数,时Java中的 在js使用时他并不是每个浏览器都有的,所以我们一般要重写一下这个函数 采用正则表达式实现startWith.endWith效果函数 String.protot ...

  7. 枚举(Enum)

    enum是一个全新的“类”. 枚举(Enum): 我们所定义的每个枚举类型都继承自java.lang.Enum类.枚举中的每个成员都是public static final的. 当您使用“enum”定 ...

  8. 小D课堂 - 零基础入门SpringBoot2.X到实战_第6节 SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener_24、深入SpringBoot过滤器和Servlet配置过滤器

    笔记 1.深入SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战(核心知识)     简介:讲解SpringBoot里面Filter讲解和使用Servle ...

  9. Chrome与chromedriver.exe的版本对应

    Chrome与chromedriver.exe的版本对应 分类专栏: pyhton3.7+selenium3   转:https://blog.csdn.net/weixin_44545954/art ...

  10. OctetString 转String

    /// <summary> /// OctetString转时间 /// </summary> /// <param name="ss">字符串 ...