-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |