题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
using System;
using System.Collections.Generic;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
int getlen(string s, int left, int right) {
while (left >= 0 && right < s.Length && s[left] == s[right]) {
left--;
right++;
}
return right - left - 1;
}
public int getLongestPalindrome(string A) {
// write code here
int maxlen = 1;
for (int i = 0; i < A.Length; i++) {
maxlen = Math.Max(maxlen, Math.Max(getlen(A, i, i), getlen(A, i, i + 1)));
}
return maxlen;
}
}
查看18道真题和解析
小天才公司福利 1165人发布