题目:

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

Subscribe to see which companies asked this question

代码:

上周出差北京一周,都没有做题,这两天继续开始,先从简单的入手。

这个题目一开始以为是找出字符串数组中最长的那个,那不很简单嘛,很快写完了,发现不对。

题目表达不是很清楚,或者是我英语不行,查了下,原来是找出字符串数组中最长的共有前缀。

于是乎,开始凑答案啦!继续用python,写起来比较简单:)

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        result = ""
        #输入[],返回""
        if len(strs) == 0:
            return result
        #flag用来判断字符串前缀是否存在于所有的字符串中
        flag=True
        #以数组中第一个字符串为例,逐步取它的前一位、两位、三位...作为前缀,去数组中所有字符串中判断
        #直至失败,说明该字符串不符合要求,于是退回一位到符合要求的字符串前缀,返回结果
        j=1
        while j in range(1,len(strs[0])+1) and flag:
            result=strs[0][0:j]
            
            for i in strs:
                if result not in i[0:j]:
                    flag = False
                    result=result[:-1]
                    break
            j += 1
        return result

发现结果居然效率还不错:

14. Longest Common Prefix的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 14. Longest Common Prefix【leetcode】

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  3. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  6. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  7. [LeetCode] 14. Longest Common Prefix

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

  8. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  9. 【LeetCode】14 - Longest Common Prefix

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

随机推荐

  1. Memcached telnet端命令

    Command Description Example get Reads a value get mykey set Set a key unconditionally set mykey 0 60 ...

  2. 搭建vpn环境:centos7+openvpn

    vpn的含义:virtual private network vpn的作用/使用场景:最常见的一个作用,你通过公网来访问某个局域网里的主机/服务,其实就是搭建一个隧道,用公网传递你的数据包,等数据包到 ...

  3. bzoj2194: 快速傅立叶之二

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. 有关bat的一些代码

    1.if语句    @echo off     if exist E:\aa.dat dir C: >> E:\ff.txt       pause     type E:\ff.txt ...

  5. TypeScript Generics(泛型)

    软件工程的一个主要部分就是构建组件,构建的组件不仅需要具有明确的定义和统一的接口,同时也需要组件可复用.支持现有的数据类型和将来添加的数据类型的组件为大型软件系统的开发过程提供很好的灵活性. 在C#和 ...

  6. jQuery下拉菜单插件Tendina.

    插件效果: 下载地址和文档: https://github.com/iprignano/tendina

  7. php操作mongodb

    <?php set_time_limit(0); $mongo = new Mongo('192.168.33.50:27017'); //连接远程主机22011端口 $db = $mongo- ...

  8. 海思h264解码库

    海思的dll,解码h264  解码后转出yuv12 dll自己百度下载  hi_h264dec.dll   hi_h264dec_w.dll   调用方法: if (H264Dec.Hi264DecA ...

  9. arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)

    IGraphicsContainerEvents Interface 如何监听 element事件? 如,当我们在Mapcontrol上添加.删除.更新了一个Element后,如何捕捉到这个事件?   ...

  10. Java递归算法——三角数字(消除递归)

    import java.io.*; // for I/O //类名:Params //属性: //方法: class Params //这个类的对象被压入栈中 { public int n; //用来 ...