Skip to content

Commit

Permalink
回文数
Browse files Browse the repository at this point in the history
  • Loading branch information
uwivbn committed Aug 27, 2019
1 parent d9e5cc0 commit ce2a647
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 5. 最长回文子串/Document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## 题目
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
**Example1:**
```
输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
```

**Example2:**
```
输入: "cbbd"
输出: "bb"
```

35 changes: 35 additions & 0 deletions 5. 最长回文子串/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# class Solution:
# def longestPalindrome(self, s: str) -> str:
# maxstr = ''
# flag = 0
# for i in range(len(s), 0, -1):
# if flag == 1:
# break
# for j in range(0, len(s)-i+1):
# substr = s[j: j+i]
# if substr == substr[::-1]:
# flag = 1
# maxstr = substr
# return maxstr

class Solution:
def longestPalindrome(self, s: str) -> str:
flag = 0
maxstr = ''
for i in range(len(s), 0, -1):
if flag == 1:
break
for j in range(0, len(s)-i+1):
substr = s[j: j+i]
if substr[0: i//2] == substr[i-i//2:][::-1]:
flag = 1
maxstr = substr
return maxstr


if __name__ == "__main__":
solution = Solution()
string = 'babad'
result = solution.longestPalindrome(string)

print(result)

0 comments on commit ce2a647

Please sign in to comment.