分类:String
考察知识点:String
最优解时间复杂度:O(n)
最优解空间复杂度:O(1)
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
代码:
我的方法:
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
#判断边界条件
if len(strs)==0:
return ""
res=strs[0]
for i in range(1,len(strs)):
while(strs[i].find(res)!=0):
res=res[:-1]
return res
讨论:
1.很简单,没啥好讨论的
简单快乐嘻嘻