【LeetCode with Python】 Longest Common Prefix

来源:互联网 发布:淘宝衣服直播主播招聘 编辑:程序博客网 时间:2024/06/08 17:45
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/longest-common-prefix/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/37335391

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


class Solution:    def findPrefix(self, str1, str2):        min_len = min(len(str1), len(str2))        for i in range(0, min_len):            if str1[i] != str2[i]:                return str1[0:i]        return str1[0:min_len]    # @return a string    def longestCommonPrefix(self, strs):        if None == strs:            return ""        n = len(strs)        if 0 == n:            return ""        elif 1 == n:            return strs[0]        prefix = strs[0]        for str in strs[1:]:            prefix = self.findPrefix(prefix, str)            if "" == prefix:                break        return prefix

0 0
原创粉丝点击