题解 | #最长公共前缀#
最长公共前缀
https://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47?tpId=295&tqId=732&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param strs string字符串一维数组 # @return string字符串 # class Solution: def longestCommonPrefix(self , strs: list[str]) -> str: # write code here if len(strs)==0: return "" L=[] for x in strs: length=len(x) L.append([x,length]) L.sort(key=lambda x: x[1]) minlenth=L[0][1] minlengthstring=L[0][0] while minlengthstring in strs: strs.remove(minlengthstring) for i in range(len(minlengthstring)): for j in strs: if minlengthstring[i]==j[i]: pass else: substring=minlengthstring[:i] return substring return minlengthstring