LeetCode-Easy014-Longest-Common-Prefix

5次阅读

共计 350 个字符,预计需要花费 1 分钟才能阅读完成。

Easy 014 Longest Common Prefix

Description:

find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. (注意要检查参数数组是否为空或 ==null)
==Example==
Input: [“flower”,”flow”,”flight”]
Output: “fl”

My Solution:

  1. for 循环找出数组中最短的那个单词,以这个单词为基准,遍历其它单词看是否 startswith 这个最短的单词,如果有一项不符合,就将最短单词进行缩减,直至所有单词都 startswith 这个(可能被缩减后的)最短单词,或者最短单词被缩减至空字符串结束
正文完
 0