演算法養成記:最長公共前綴

  • 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上測試。當然答案不唯一,由於能力有限,實現方法不一定是最好的,也希望各位小夥伴一起來學習分享~

Tags: