题目:

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. 吉特仓库管理系统- 斑马打印机 ZPL语言的腐朽和神奇

    上一篇文章说到了.NET中的打印机,在PrintDocument类也暴露一些本质上上的问题,前面也提到过了,虽然使用PrintDcoument打印很方便.对应条码打印机比如斑马等切刀指令,不依赖打印机 ...

  2. 《DOM Scripting》 - 阅读笔记

    DOM Scripting - Web Design with JavaScript and the Document Object Model,Jeremy Keith中文名:JavaScript ...

  3. AC自动机(BZOJ1030)

    #include <cstdio> #include <queue> #include <cstring> using namespace std; ; int c ...

  4. K - 迷宫问题

    /*定义一个二维数组:  int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, ...

  5. MySQL5.6忘记root用户名和密码

    首先我们要做的是关闭数据库,还好这个只是一个开发库,要是生产库的话使用另外一种方法修改root用户名和密码,我在另一篇文章有记载 然后我们跳过网络,跳过授权表,这个时候只有本机可以登录了,外部机器就不 ...

  6. js base64加密,后台解密

    这是为了解决页面发送post请求,传输密码,在页面的控制台可以看到密码的明文,所以先用base64把要传输的密码转换为非明文,然后在后台解密处理. base64encode.js // base64加 ...

  7. PRINCE2

    首先要说的是,我这篇体会是针对一定的背景的,不能算是一种通用的管理方式,只能是我自己的经验总结,能给大家平常的管理提供一点思路,我就很满足了.先说说背景,我所在公司做的是大型桌面应用软件,简单点说就是 ...

  8. ASP.NET MVC 必须设置 ErrorMessageString 或 ErrorMessageResourceName,但不能同时设置二者。

    解决方案: 1.此错误是指你的验证错误信息为空(required="")和required提示信息一致,引发的错误. 简单的来说就是两个验证错误提示消息一样了. 2.修改提示消息解 ...

  9. Sql简单封装

    public class SqlHelper { public static string SqlConnectionStr = ConfigurationManager.ConnectionStri ...

  10. iOS7时代我们用什么来追踪和识别用户?

    要识别用户,首先就是要选择一个标识符,通过这个标识符来识别这个用户的设备(而不是用户),这个标识符要能够保证一个设备上返回的值是一样的,并且在其他设备上不会出现相同的值. 在iOS7之前,曾经有过很多 ...