zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):字符串类:第14题:最长公共前缀:编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。

    题目:
    最长公共前缀:编写一个函数来查找字符串数组中的最长公共前缀。  如果不存在公共前缀,返回空字符串 ""。

    说明:

    所有输入只包含小写字母 a-z 。

    思路:

    思路较简单。

    程序:

    class Solution:
        def longestCommonPrefix(self, strs: List[str]) -> str:
            if not strs:
                return ""
            length = len(strs)
            if length == 1:
                return strs[0]
            result = strs[0]
            for index in range(1, length):
                if strs[index] == 0 or not result:
                    return ""
                length_min = min(len(result), len(strs[index]))
                auxiliary = ""
                for index2 in range(length_min):
                    if result[index2] == strs[index][index2]:
                        auxiliary = auxiliary + result[index2]
                    else:
                        break
                result = auxiliary
            return result
  • 相关阅读:
    使用servletContext和类加载器加载文件
    servletConfig和servletContext的应用
    java中的正则表达式(一)
    servlet的生命周期
    servlet的基本原理
    java中类加载机制
    java中的动态代理(三)
    Navicat Premium 连接Oracle 数据库
    使用SqlServer2005 Service Broker 和 SqlDependency类提供数据更改的通知
    WebService简单使用
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12839788.html
Copyright © 2011-2022 走看看