题目:Write a function to find the longest common PRefix string amongst an array of strings.
思路:找出最小长度的字符串,逐个子集判断,判断时可以使用set,最后判断set元素是否为1个即可。
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if len(strs)==0:return '' ind=0;minLen=len(strs[0]) for i in range(len(strs)): if len(strs[i])<minLen: ind = i;minLen=len(strs[i]) r = '' for i in range(minLen): s = set() for j in strs: s.add(j[:i+1]) if len(s)>1:return r r = s.pop() del s return r
新闻热点
疑难解答