暴力解
func longestPalindrome(s string) string {
if IsPalindrome(s) {
return s
}
maxLength := 0
var ans string
for width := 1; width < len(s); width++ {
for i := 0; i < len(s)-width+1; i++ {
temp := s[i : i+width]
if IsPalindrome(temp) {
if len(temp) > maxLength {
maxLength = len(temp)
ans = temp
}
}
}
}
return ans
}
func IsPalindrome(s string) bool {
if len(s) == 0 || len(s) == 1 {
return true
}
i := 0
j := len(s) - 1
for i < (len(s) / 2) {
if s[i] != s[j] {
return false
}
i++
j--
}
return true
}