Jan 132021
 

题目很简单,寻找开头相同的字母,看看最大是多少。

https://leetcode.com/problems/longest-common-prefix/

每次看到这种题目,都有一种想法,如果python,有一个内置函数搞定这种看上去比较普遍的问题,多好。没想到这个问题,就是这种情况。居然有内置的函数,直接搞定。

Input: strs = [“flower”,”flow”,”flight”]
Output: “fl”

https://medium.com/@d_dchris/10-methods-to-solve-the-longest-common-prefix-problem-using-python-leetcode-14-a87bb3eb0f3a

上面的朋友用了10种方法来解答,我最喜欢的,还是最后一种。

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        return os.path.commonprefix(strs)

 Leave a Reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.