LeetCode第14题:Longest Common Prefix总结

题目

Write a function to find the longest common prefix string amongst an array of strings.

  • 大意: 写一个函数,找到一组字符串的公共最长前缀。

思路

我的:定义一个不断更新的变量,存储当前的最长前缀,循环时加以一定的优化,比如如果下一个字符串比当前的前缀还长,那么可以截断前缀后在进行比较。
参考的:这道题在discuss上看到了一些运用了python语法的解法,用到了zip,reduce,set等python特有的语法,顺便贴出。

代码

原创(Python):

Python(用了zip)

(《A pythonic solution, 52 ms》)

Python(用了reduce)

(《Simple Python solution》)

发表回复

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