算法养成记:最长公共前缀

  • 2020 年 3 月 10 日
  • 筆記

LeetCode14

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

Example 2:

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

Note:

All given inputs are in lowercase letters a-z.

在实际测试里,三种算法的结果都是递增的

执行用时分别是:1ms,1ms,0ms

内存消耗分别是:37.8MB,38.8MB,37.5MB

这一版文案您还觉得满意吗?
哪里不太对,但又说不上来。

数据结构和算法一直都是程序员面试重点。写好每一个方法,每一个接口,程序的效率也会越来越高。为了学习和巩固数据结构和算法,我们特别创作了《呆萌程序员–明明凯凯算法养成记》,每天更新一篇数据结构知识点或者刷一道LeetCode题目。算法都会在LeetCode上测试。当然答案不唯一,由于能力有限,实现方法不一定是最好的,也希望各位小伙伴一起来学习分享~