【发布时间】:2018-03-12 19:11:19
【问题描述】:
我正在尝试使用this 之类的递归来获得回文,但我发现这样做非常昂贵,因为当我使用substring() 方法时它会创建新字符串,就像在我的代码中一样:
public static boolean isPalindrome(String s) {
if (s.length() <= 1)
return true;
else if (s.charAt(0) != s.charAt(s.length() - 1))
return false;
else return isPalindrome(s.substring(1, s.length() - 1));
}
有没有其他方法可以提高效率?
【问题讨论】: