【发布时间】:2015-04-26 03:29:12
【问题描述】:
编写一个名为 isReverse("word1", "word2") 的递归方法,它接受两个字符串作为参数,如果两个字符串包含 相同的字符序列,但顺序相反,忽略大小写,否则返回 false。 比如调用:
isReverse("Desserts", "Stressed")
将返回 true。 【所以压力大的时候吃甜点?】 Null、empty 和一个字母的字符串也将返回 true(如果两个参数的值相同)。 这是家庭作业,我无法使此代码正常工作。无论我做什么,它都会返回 true。
public static boolean isReverse(String word1, String word2)
{
if(word1 == null || word2 == null)
{
if(word1!= null && word2 != null)
{
return false;
}
return false;
}
else if(word1.length() == word2.length())
{
String firstWord = word1.substring(0, word1.length());
String secondWord = word2.substring(word2.length()-1);
if (firstWord.equalsIgnoreCase(secondWord))
{
return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1));
}
}
return true;
}
【问题讨论】:
-
String 或 StringBuffer 类有反向方法
-
@SrinathGanesh - StringBuilder 有逆向,但我们必须假设他没有被教过 Builder 类。我已经完成了 3 年的大学生活而没有被提及。
-
鉴于这是作业,我猜想使用内置函数完成 95% 的作业是禁止的。
-
你的第二个单词只有 1 个字符