classSolution{
public:intreverse(int x){
int ret=0;while(x!=0){
int pop=x%10;
x/=10;if(ret>INT_MAX/10||(ret==INT_MAX && pop>7))return0;if(ret<INT_MIN/10||(ret==INT_MIN && pop<-7))return0;
ret=ret*10+pop;}return ret;}};
5. 最长回文子串
中心扩散法
DP(待办)
classSolution:deflongestPalindrome(self, s:str)->str:deffunc(s, i, j):while0<= i and j <len(s)and s[i]== s[j]:
i -=1
j +=1
i +=1
j -=1return i, j
sa, sb =0,0for i inrange(len(s)):
a, b = func(s, i, i)if b - a > sb - sa:
sa, sb = a, b
if i <len(s)-1:
a, b = func(s, i, i +1)if b - a > sb - sa:
sa, sb = a, b
return s[sa:(sb +1)]