读书人

[讨论]判断是否回文忽略标点和空格部

发布时间: 2012-02-21 16:26:23 作者: rapoo

[讨论]判断是否回文,忽略标点和空格部分怎么写?
public class Palindrome
{
static private String text;
static private int firstIndex;
static private int lastIndex;

public static boolean isPalindrome(String text, int firstIndex,int lastIndex)
{
text = text.toLowerCase();
firstIndex = 0;
lastIndex = text.length()-1;

for( int i=0; i < text.length(); i++)
{
char letter = text.charAt(i);
if(character.isLetterOrDigit(letter)

这里 我要忽略标点和空格部分,但是怎么继续写下去呢??

}

if (firstIndex > = lastIndex)

return true; // Base Case


else if (text.charAt(firstIndex) != text.charAt(lastIndex))

return false;
else

{
return isPalindrome(text, firstIndex + 1, lastIndex -1);
}



}

}


[解决办法]
有个思路。给你个思路。。
你可以把你字符串中的 标点和空格都换掉。换成“”
第二中方法你可拆分字符串。拆分点就是有标点和空格的地方。然后去掉然后在链接组成新的字符串。
[解决办法]
扫描一下字符串嘛,不是标点或空格就填到新字符串中去,是的话就不要填

读书人网 >J2SE开发

热点推荐