Skip to content

Latest commit

 

History

History
47 lines (27 loc) · 1.04 KB

0014.longest-common-prefix.md

File metadata and controls

47 lines (27 loc) · 1.04 KB

0014.Longest-Common-Prefix

Problem

Description

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.

Tags: String

题解

思路1

题意是让你从字符串数组中找出公共前缀,我的想法是找出最短的那个字符串的长度 minLen,然后在 0...minLen 的范围比较所有字符串,如果比较到有不同的字符,那么直接返回当前索引长度的字符串即可,否则最后返回最短的字符串即可。

思路2

结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:awesome-golang-algorithm