Leetcode 14.最长公共前缀 Python
2021/12/5 14:17:26
本文主要是介绍Leetcode 14.最长公共前缀 Python,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
class Solution: def longestCommonPrefix(self, strs): longest="" for i in range(min([len(s) for s in strs])): yn=[] for s2 in strs: yn.append(s2.startswith(strs[0][:i+1])) if not (False in yn): longest=strs[0][:i+1] return longest
解题思路:
遍历的次数由最短的字符串决定
判断每个字符串是否有同样的前缀
是则把最长前缀设置为当前前缀
最后返回即可
关注我,在Leetcode专栏中查看更多题目的解题思路吧!
这篇关于Leetcode 14.最长公共前缀 Python的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python