题目:

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. java IO流复制图片

    一.使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // Str ...

  2. ecliplse高亮显示选中的相同变量

    选择:windows-> preferences->java->Editor->Mark Occurences 选择最上的复选框,下面的就有很多了. 其中的Local vari ...

  3. js获取域名

    <script language="javascript">//获取域名host = window.location.host;host2=document.domai ...

  4. 江太公:javascript count(a)(b)(c)(d)运行过程思考

    昨天,我弟抛给我一个js的题,使用类似标题那样的调用方法计算a*b*c*d以致无穷的实现方法.思考了半天,终于理清了它的运行过程,记录于下: 函数体: <!DOCTYPE html> &l ...

  5. BZOJ 1503: [NOI2004]郁闷的出纳员

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 10526  Solved: 3685[Submit][Stat ...

  6. noip2016十连测round2

    A: Divisors 题意:给定 m 个不同的正整数 a 1 ,a 2 ,...,a m ,请对 0 到 m 每一个 k 计算,在区间 [1,n] 里有多少正整数 是 a 中恰好 k 个数的约数. ...

  7. Bzoj1176 [Balkan2007]Mokia

    Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2000  Solved: 890 Description 维护一个W*W的矩阵,初始值均为S.每次操作 ...

  8. BZOJ4570: [Scoi2016]妖怪

    题目传送门 4570: [Scoi2016]妖怪 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 491 Solved: 125 [Submit][Sta ...

  9. 树莓派2安装使用小米WIfi(360 小度 腾讯wifi)

    更新2015年11月16日,jessie内核版本号4.1.13(uname -a 可以查看)直接可以驱动MT7601U,无需手动编译. 截止2015-4-6,本文基于树莓派2,raspbian,内核版 ...

  10. linux 乱码

    xwindow终端乱码 在/etc/sysconfig/i18n文件的最后一行添加如下内容: export LC_ALL="zh_CN.GB18030" vi /etc/sysco ...