Skip to content

Commit

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

[4. Median of Two Sorted Arrays](Solution/1-50/4.md)

[5. Longest Palindromic Substring](Solution/1-50/5.md)

[7. Reverse Integer](Solution/1-50/7.md)

[8. String to Integer (atoi)](Solution/1-50/8.md)
Expand Down
34 changes: 34 additions & 0 deletions Solution/1-50/5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#5. Longest Palindromic Substring
[题目链接](https://leetcode.com/problems/longest-palindromic-substring/#/description)
```python
class Solution(object):
start = maxLength = 0
def findPalindrome(self, s, i, j):
"""
:param s: str
:param i: int
:param j: int
:return:
"""
while i >= 0 and j < len(s) and s[i] == s[j]:
i -= 1
j += 1
length = j - i - 1
if length > self.maxLength:
self.maxLength = length
self.start = i + 1

def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) <= 2:
return s
# 遍历字符串计算,分奇数情况和偶数情况
for i in range(len(s)):
self.findPalindrome(s, i, i)
self.findPalindrome(s, i, i + 1)
return s[self.start: self.start + self.maxLength]

```

0 comments on commit e7d18f4

Please sign in to comment.