[LeetCode][Python]14: Longest Common Prefix

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘https://oj.leetcode.com/problems/longest-common-prefix/14: Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.===Comments by Dabay===注意边界条件,如果strs为空,直接返回空字符串。

初始化共同前缀为空字符串。如果一个字符出现在每个字符的相应位置就把这个字符加到共同前缀中。‘‘‘

class Solution:    # @return a string    def longestCommonPrefix(self, strs):        if len(strs) == 0:            return ""        common_pre = ""        i = 0        while True:            to_compare = ""            for s in strs:                if i >= len(s):                    return common_pre                if to_compare == "":                    to_compare = s[i]                    continue                if s[i] != to_compare:                    return common_pre            else:                common_pre = common_pre + to_compare            i = i + 1

def main():    s = Solution()    strs = ["abcdef", "abc", "abcd"]    print s.longestCommonPrefix(strs)

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-10-11 04:35:48

[LeetCode][Python]14: Longest Common Prefix的相关文章

【LeetCode】14 - Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. Solution: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { //runtime:4ms 4 string str=""; 5 if(strs.empty())return

leetCode 14. Longest Common Prefix 字符串

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 题目大意:求一组字符串的最长前缀. 代码如下: class Solution { public:     string longestCommonPrefix(vector<string>& strs) {         if(strs.size() == 0)

14. Longest Common Prefix【leetcode】

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 寻找一个数组中最长的公共前缀 例如["baaa","caaabbb","aaaa"]输出"aaa" 结题思路: 判断非空的情况在进行计算 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中 用第一

leetcode -- 刷效率 Longest Common Prefix

题目描述: Write a function to find the longest common prefix string amongst an array of strings. 很简单的一道题目,但是我写了3个不一样的版本,运行时间确实数倍之差..贴代码如下: 版本1: 这个版本的运行时间为  44 ms 版本2: 这个版本的运行时间为  16 ms 两者之间的区别便是:有无创建string ret返回字符串..  [updated]版本3:使用字符数组,时间同样是16ms,代码如下:

No.14 Longest Common Prefix

No.14 Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 求一组string的最长公共前缀想法:找到最短的那个,然后依次对比 典型的字符串操作,从前向后比较即可 1 #include "stdafx.h" 2 #include <string> 3 #include <vector> 4 #i

leedCode练题——14. Longest Common Prefix

1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight&qu

LeetCode 14 Longest Common Prefix (C,C++,Java,Python)

Problem: Write a function to find the longest common prefix string amongst an array of strings. Solution: 时间复杂度O(n) 题目大意: 给一个字符串数组,要找到这些字符串的最大前缀公共子串. 解题思路: 既然是公共子串,那每个字符串肯定都包含有,并且在头部,首先把第一个字符串作为默认最大,然后依次与后边每一个字符串对比,计算所有的最大匹配长度,长度最小的就是 Java源代码(用时263ms

14. Longest Common Prefix [easy] (Python)

题目链接 https://leetcode.com/problems/longest-common-prefix/ 题目原文 Write a function to find the longest common prefix string amongst an array of strings. 题目翻译 写个函数,找出一个字符串数组中所有字符串的最长公共前缀. 题目描述不清晰...补充几个例子,比如: {"a","a","b"} 返回 &qu

LeetCode记录之14——Longest Common Prefix

本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest common prefix string amongst an array of strings. 编写一个函数来查找字符串数组中最长的公共前缀字符串. 1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3