I am so stupid!!! Spent long time on that and cannot fix the bugs in short time.
It has two solutions: the first is treat each element as the center, then expand base on the center and check the validation. The second is use dp, which equation is dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1]; which means whether the string from i to j is match or not. However, both of them spend lots of time to debug.
Which the first one blocked me because I miss a case when expanding, which is if the left and right is incorrect at first time, we should not return its minus value.
The second is I swap i and j when calculate length.