LeetCode-Easy014-Longest-Common-Prefix

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这个(可能被缩减后的)最短单词,或者最短单词被缩减至空字符串结束

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理