[Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697903.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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.
Note:
All given inputs are in lowercase letters a-z
.
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""
。
示例 1:
输入: ["flower","flow","flight"] 输出: "fl"
示例 2:
输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z
。
16ms
1 class Solution { 2 func longestCommonPrefix(_ strs: [String]) -> String { 3 let minStr = try? strs.min(by: { $0.count < $1.count }) 4 guard var minS = minStr! else { 5 return "" 6 } 7 8 var endIndex = minS.endIndex 9 for str in strs { 10 if (str.hasPrefix(minS)) { 11 continue 12 } 13 while endIndex > minS.startIndex && !str.hasPrefix(minS) { 14 endIndex = minS.index(before: endIndex) 15 minS = String(minS[minS.startIndex..<endIndex]) 16 } 17 } 18 return minS 19 } 20 }
20ms
1 class Solution { 2 func longestCommonPrefix(_ strs: [String]) -> String { 3 4 if strs.count == 0 { 5 return "" 6 } 7 var prefix = strs[0] 8 for i in 0..<strs.count { 9 while strs[i].hasPrefix(prefix) == false { 10 prefix = prefix.substring(to: prefix.index(before: prefix.endIndex)) 11 if prefix.isEmpty { 12 return "" 13 } 14 } 15 } 16 return prefix 17 } 18 }
24ms
1 class Solution { 2 func longestCommonPrefix(_ strs: [String]) -> String { 3 guard strs.count > 0 else { return "" } 4 var shortestPrefix = strs.min{ $0.count < $1.count }! 5 print(shortestPrefix) 6 for s in strs { 7 while shortestPrefix.count > 0 , !s.hasPrefix(shortestPrefix) { 8 shortestPrefix.removeLast() 9 } 10 } 11 return shortestPrefix 12 } 13 }
28ms
1 class Solution { 2 func longestCommonPrefix(_ strs: [String]) -> String { 3 4 var res = "" 5 if strs.isEmpty { 6 7 return res 8 } 9 10 res = strs[0] 11 12 for i in 1..<strs.count { 13 14 var find = Array(strs[i]) 15 var current = Array(res) 16 res = "" 17 for i in 0..<find.count { 18 19 if i >= current.count { 20 break 21 } 22 23 if current[i] != find[i] { 24 25 break 26 } else { 27 28 res += String(current[i]) 29 } 30 } 31 32 } 33 34 return res 35 } 36 }
32ms
1 class Solution { 2 func longestCommonPrefix(_ strs: [String]) -> String { 3 guard let first = strs.first else { return "" } 4 5 var currentPrefix = "" 6 var bestPrefix = "" 7 8 for letter in first { 9 currentPrefix.append(letter) 10 11 for word in strs { 12 if !word.hasPrefix(currentPrefix) { 13 return bestPrefix 14 } 15 } 16 17 bestPrefix = currentPrefix 18 } 19 20 return bestPrefix 21 } 22 }