0005. 最长回文子串 #54
utterances-bot
started this conversation in
Comments
Replies: 0 comments 3 replies
-
class Solution:
def longestPalindrome(self, s: str) -> str:
res = ''
for i in range (len(s)):
start = max(0, i - len(res) - 1)
temp = s[start : i + 1]
if temp == temp[:: -1]:
res = temp
else:
temp = temp[1:]
if temp == temp[::-1]:
res = temp
return res |
Beta Was this translation helpful? Give feedback.
0 replies
-
附上C++代码关于 class Solution {
public:
string longestPalindrome(string s) {
bool dp[N][N] = {false};
int n = s.size();
if(n <= 1) return s;
int maxlen = 1;
int start = 0;
for(int j = 1; j < n; ++j) {
for(int i = 0; i < j; ++i) {
if(s[i] == s[j]) {
if(j-i+1 < 4) dp[i][j] = true;
else dp[i][j] = dp[i+1][j-1];
}
if(dp[i][j] && j-i+1 > maxlen) {
maxlen = j-i+1;
start = i;
}
}
}
return string(s.begin()+start, s.begin()+start+maxlen);
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
-
您好 请问能解释一下这段代码吗…我实在是没搞懂,谢谢!! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
0005. 最长回文子串 | 算法通关手册
https://algo.itcharge.cn/Solutions/0001-0099/longest-palindromic-substring/
Beta Was this translation helpful? Give feedback.
All reactions