1. 程式人生 > >LeetCode Notes_#14 Longest Common Prefix

LeetCode Notes_#14 Longest Common Prefix

LeetCode Notes_#14 Longest Common Prefix

LeetCode 

Contents

Problem

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"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Solution

思路

尋找最長的公共字首,我的想法是直接從第一個字母開始遍歷,設定一個flag代表是否有共同的字首。

#try:擷取一個字串的一部分
a='string'
a[0:2]
'st'
  1. #遇到一個大小寫問題,意識到動態語言的缺點了... 
  2. Input=["flower","flow","flight"
  3. InputLen=[] 
  4. MinLen=0 
  5. flag=1 
  6. TmpList=[]#用於儲存每個字串的字首 
  7. for i in range(len(Input)): 
  8. InputLen.append(len(Input[i])) 
  9. MinLen=min(InputLen) 
  10.  
  11. for j in range(1,MinLen): 
  12. # print(flag) 
  13. TmpList=[] 
  14. if flag:#前面的字首相同了,我才繼續往後去嘗試 
  15. for k in range(len(Input)):#遍歷每個單詞 
  16. TmpList.append(Input[k][0:j])#依次取每個單詞的前面j個字元,加入列表 
  17. print(TmpList) 
  18. if len(set(TmpList))!=1
  19. flag=0#這時剛好開始出現不同,那麼前j-1個相同 
  20. print (Input[0][0:j-1]) 
  21.  
['f', 'f', 'f']
['fl', 'fl', 'fl']
['flo', 'flo', 'fli']
fl
#一些corner case,emmmm
[]
[""]
["",""]
["a","aa"]
["c","c"]
[""][0]#這種情況就不可以用二重下標了
''
'a'[0]

'a'
'a'[0:1]
'a'

解答

  1. class Solution(object): 
  2. def longestCommonPrefix(self, strs): 
  3. """ 
  4. :type strs: List[str] 
  5. :rtype: str 
  6. """ 
  7. if strs==[]: 
  8. return '' 
  9. if '' in strs: 
  10. return '' 
  11. if len(strs)==1
  12. return strs[0]  
  13. if len(set(strs))==1
  14. return strs[0
  15.  
  16. InputLen=[] 
  17. MinLen=0 
  18. flag=1 
  19. TmpList=[]#用於儲存每個字串的字首 
  20. for i in range(len(strs)): 
  21. InputLen.append(len(strs[i])) 
  22. MinLen=min(InputLen) 
  23. # print MinLen 
  24. # print range(1) 
  25.  
  26. for j in range(MinLen): 
  27. # print(flag) 
  28. TmpList=[] 
  29. if flag:#前面的字首相同了,我才繼續往後去嘗試 
  30. for k in range(len(strs)):#遍歷每個單詞 
  31. TmpList.append(strs[k][0:j+1])#依次取每個單詞的前面j個字元,加入列表 
  32. # print(TmpList) 
  33. # print j,k,TmpList 
  34. # print len(set(TmpList)) 
  35. if len(set(TmpList))!=1
  36. flag=0#這時剛好開始出現不同,那麼前j-1個相同 
  37. # print j 
  38. if j>=1
  39. return strs[0][0:j]  
  40. else
  41. return '' 
  42. elif (len(set(TmpList))==1 and j==MinLen-1): 
  43. return TmpList[0

28ms,faster than 57.76%
這些corner case有點煩人...這程式碼有點辣雞,很多的for還有if else...

高票答案

  1. class Solution: 
  2. # @return a string 
  3. def longestCommonPrefix(self, strs): 
  4. if not strs: 
  5. return "" 
  6.  
  7. for i, letter_group in enumerate(zip(*strs)): 

    相關推薦

    LeetCode Notes_#14 Longest Common Prefix

    LeetCode Notes_#14 Longest Common Prefix LeetCode  Contents Problem Solution 思路

    LeetCode14. Longest Common Prefix - Java實現

    文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Write a function to find the longest common prefix string amongst an arr

    leetcode14. Longest Common Prefix 最長公共字首

    1. 在使用C++ String 類時,要加上 #include <iostream> 和 # include <string> 而不是 <string.h>. string s6(5,'A');  表示 生成一個字串包含 5 個 ‘A

    LeetCode14. Longest Common Prefix

    1. 題目描述: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, retu

    LeetCode 14. Longest Common Prefix

    color pan solution 裏的 string 內容 自己的 所有 一定的 Write a function to find the longest common prefix string amongst an array of strings. 題目的描述

    [LeetCode] 14. Longest Common Prefix 最長共同前綴

    not turn npr longest stc tostring ref 執行 common Write a function to find the longest common prefix string amongst an array of strings. 這

    leetcode-14. Longest Common Prefix

    題目型別: 字串 題意: Write a function to find the longest common prefix string amongst an array of strings. 找出一個字串陣列中所有字串的最長共同==字首==。 字串API: =

    LeetCode 14 Longest Common Prefix 解題報告

    描述 給定一個字串集合,需要求出這些字串的公共字首 樣例 Input: ["flower","flow","flight"] Output: "fl" 思路 首先獲得最短字串的長度,按照這個長度進行外層遍歷,之後以此遍歷每個字串,看是否滿足相等的條件。如果採用的是 s[i] == s[i-1

    LeetCode 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 stri

    leetcode 14 Longest Common Prefix C++實現

    leetcode 14 Longest Common Prefix C++實現 Write a function to find the longest common prefix string amongst an array of strings. If

    LeetCode--14. Longest Common Prefix

    題目連結:https://leetcode.com/problems/longest-common-prefix/ 這是一個easy的題目,但是涉及的思維方式和方法還有一些用的比較少的高階資料結構都是值得討論研究的,意味深長。LeetCode的前兩百道題目各個都是經典,引人思考。這個題目要求一個

    [LeetCode][14]Longest Common Prefix解析 兩種演算法和底層原始碼的深入對比-Java實現

    Q: Write a function to find the longest common prefix string amongst an array of strings. A: 這題的大概意思就是說給你一組字串找出其中最長的哪個通用的前綴出來。這個東西不難找,但是如

    14. Longest Common Prefix

    nbsp vector prefix fin else return col end 公共前綴 Write a function to find the longest common prefix string amongst an array of strings.

    LeetCode】014. Longest Common Prefix

    mon 暴力 turn fin fix HA efi res div Write a function to find the longest common prefix string amongst an array of strings. 題解:   簡單的暴力遍歷

    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 "

    14. Longest Common Prefix C++

    採用縱向遍歷,即對第一個字串,取出第一個字元,檢查是否出現在隨後每一個字串中,以此類推。當遍歷完成或有一個字串不符合要求,直接return。 class Solution { public: string longestCommonPrefix(vector<string>&

    【演算法】LeetCode演算法題-Longest Common Prefix

    這是悅樂書的第146次更新,第148篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第5題(順位題號是14),給定一個隨機的字串陣列,查詢這些字串元素的公共字首字串,如果沒有則返回空串。其中,字串陣列中的元素都是由小寫字母a-z之間

    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 emp

    14. Longest Common Prefix(最長公共字首) —— Java

    Write a function to find the longest common prefix string amongst an array of strings. Example: Given ["abc","ab","abcd"], return "a

    LeetCode】#14最長公共字首(Longest Common Prefix)

    【LeetCode】#14最長公共字首(Longest Common Prefix) 題目描述 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 “”。 示例 示例 1: 輸入: [“flower”,“flow”,“flight”] 輸出: “