【发布时间】:2013-09-18 06:04:08
【问题描述】:
我目前正在为我在高中上的一门课用 Java 编写回文测试器。我曾向我的老师寻求帮助,他也很困惑。我希望 stackoverflow 上的社区可以帮助我。谢谢。
public class Palindrome
{
private String sentence;
public Palindrome(String s)
{
sentence = s;
}
public boolean isPalindrome()
{
if(sentence.length() <= 1)
{
return true;
}
if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
isPalindrome();
}
else
return false;
}
}
【问题讨论】:
-
这是一种非常低效的检查回文的方法(因为每个
substring调用都会创建一个新字符串),这有关系吗?对字符串进行简单的 for 循环会好得多。
标签: java return palindrome