Skip to content

Commit

Permalink
a14
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhengwei committed May 23, 2017
1 parent e7d18f4 commit 69d7e4e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ leetcode 题解

[9. Palindrome Number](Solution/1-50/9.md)

[14. Longest Common Prefix](Solution/1-50/14.md)

[19. Remove Nth Node From End of List](Solution/1-50/19.md)

[21. Merge Two Sorted Lists](Solution/1-50/21.md)
Expand Down
26 changes: 26 additions & 0 deletions Solution/1-50/14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 14. Longest Common Prefix
[题目链接](https://leetcode.com/problems/longest-common-prefix/#/description)
```python
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 0:
return ''
prefix = strs[0]
for data in strs:
length = len(prefix)
# 如果前缀的长度比字符串长,则交换
if len(data) < length:
prefix , data = data, prefix
length = len(prefix)
while data[:length] != prefix:
prefix = prefix[:-1]
length -= 1
# 如果长度为0,意味着没有公共前缀,所以返回0
if length == 0:
return ''
return prefix
```

0 comments on commit 69d7e4e

Please sign in to comment.