1 Star 0 Fork 0

wd6/LeetCode-1

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
longest-common-prefix.py 647 Bytes
Copy Edit Raw Blame History
# Time: O(n * k), k is the length of the common prefix
# Space: O(1)
# Write a function to find the longest common prefix string
# amongst an array of strings.
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
for i in xrange(len(strs[0])):
for string in strs[1:]:
if i >= len(string) or string[i] != strs[0][i]:
return strs[0][:i]
return strs[0]
if __name__ == "__main__":
print Solution().longestCommonPrefix(["hello", "heaven", "heavy"])
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

Search