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. 代码实现

 1 public class LongestCommenPrefix14 {
 2
 3     public static void main(String[] args) {
 4         String[] strs = {"weeer", "weer", "wer"};
 5         System.out.println(LongestCommenPrefix14.longestCommonPrefix(strs));
 6     }
 7
 8     public static String longestCommonPrefix(String[] strs) {
 9         if (strs.length == 0)
10             return "";
11         String prefix = strs[0];
12         for (int i = 1; i < strs.length; i++) {
13             while (strs[i].indexOf(prefix) != 0) {   //与假定的公共前缀不匹配
14                 prefix = prefix.substring(0, prefix.length() - 1); //缩短假定的公共前缀
15                 if (prefix.equals(""))
16                     return "";
17             }
18         }
19         return prefix;
20     }
21 }

转载于:https://www.cnblogs.com/huiAlex/p/8072229.html

LeetCode:14. Longest Commen Prefix(Easy)相关推荐

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

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

  2. 【leetcode】Remove Linked List Elements(easy)

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

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

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

  4. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  5. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  6. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  7. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  8. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  9. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

最新文章

  1. 地理信息系统论坛_高端论坛西南交通大学朱庆教授:应急测绘智能服务关键技术及重大应用...
  2. obs可以推到中转服务器吗,能否使用OBS(Open Broadcaster Software)、FMLE(Flash Media Live Encoder)等第三方软件进行推流?...
  3. [Leetcode][第1025题][JAVA][除数博弈][数学][递推]
  4. Linux多线程同步
  5. TrueNAS Enterprise是什么
  6. target-action设计模式--主要为Button的方法重写
  7. FZU 1894 志愿者选拔 - 单调队列
  8. 在idea中使用debug
  9. [IntelliJ IDEA + EduTools] 从零开始编写一套 Java 教材(一)
  10. 解决ValueError: x and y must have same first dimension,
  11. php发送邮件pop3,php 发送邮件与pop3邮件登录代码
  12. 分了很多节的word文档,使用尾注插入的参考文献,最后怎么在参考文献之后加入致谢?
  13. 在支付宝中开通信用卡快捷支付
  14. 在线教育未来的发展前景如何?
  15. IMX6Q的硬编码(VPU)的工程代码移植
  16. http://zjj2816.cnblogs.com/?Contact=1
  17. 计算机冗余,惯性导航计算机系统冗余设计
  18. Unity3D教程:如何利用Shader实现钻石渲染效果
  19. 交换安全 STP生成树
  20. linux发送http请求

热门文章

  1. 项目实践——语音计算器
  2. win10-yolov5环境搭建
  3. 斐讯音箱控制扫地机器人_斐讯扫地机器人app
  4. 根据视频URL解析视频信息(本地|网络)
  5. 为什么程序中用双引号括起来的宏在预处理的时候是不会被宏替换的。
  6. Java实现HMacMD5加密,用于淘宝客JS 组件 API 调用时生成 sign 的签名
  7. ai替换混合轴例子_可解释的vs可解释的AI:一个直观的例子
  8. 两个求和符号如何用计算机,计算:两个求和符号∑∑怎么办
  9. 【LeetCode】40. 组合总和 II (JavaScript)
  10. 沐风:做一个会自动赚钱的小程序