我已经看到了其他的递归回文问题而且我已经看到了答案,但我得到的答案有点不同。它需要能够检查带有空格和标点符号的字符串并忽略它们(我知道,它们在技术上不是回文),所以像“女士,我是亚当”这样的字符串应该返回true,但我的程序不是。这就是我所拥有的:
public static boolean isPalindrom(String s){
System.out.println(s);
if (!Character.isLetter(s.charAt(0))){
isPalindrom(s.substring(1,s.length()));
}
if (!Character.isLetter(s.charAt(s.length()-1))){
isPalindrom(s.substring(0,s.length()-1));
}
if (s.length() == 1){
return true;
}
if (s.length() == 2){
if (s.substring(0,1).equalsIgnoreCase(s.substring(s.length()-1))){
return true;
}
else
return false;
}
if (!(s.substring(0,1).equalsIgnoreCase(s.substring(s.length()-1)))){
return false;
}
return isPalindrom(s.substring(1,s.length()-1));
}
问题是,一旦它开始展开递归,包含空格和标点符号的字符串就开始返回false。我不知道该怎么做。我现在一直在努力尝试不同的解决方案大约一个小时。
P.S。我试图不使用正则表达式来删除空格和标点符号等。
答案 0 :(得分:4)
你没有返回前两个isPalindrom
的结果(检查现在开始失败的案例......):
if (!Character.isLetter(s.charAt(0))){
return isPalindrom(s.substring(1,s.length()));
}
if (!Character.isLetter(s.charAt(s.length()-1))){
return isPalindrom(s.substring(0,s.length()-1));
}
答案 1 :(得分:0)
当你到达一个时,你只需要继续前进。像这样。我留给你填写空白(特殊情况,lastNonIgnoredChar等)
char[] ignored = new char[] { ',' , ' ', '.'};
int firstNonIgnoredChar(String s) {
for(int i = 0; i < s.length(); i++) {
boolean found = false;
for(char c : ignored) {
if( c == s.charAt(i) ) found = true;
}
if(!found) return i;
}
return -1; // no good characters
}
boolean isPal(String s) {
int first = firstNonIgnoredChar(s);
int last = lastNonIngoredChar(s);
if(s.length() == 0) return true;
if(s.length() == 1) return true;
return first < last
&& s.charAt(first) == s.charAt(last)
&& isPal(s.substring(first + 1, last - 1);
}
答案 2 :(得分:0)
在最坏情况下(完全搜索)在O(n / 2)下运行的非递归方法。当String是LONG的时候,这是更高效的......这是实现......
class PalindromeClass {
/**
* This method will run under or at O(n/2) with n = sentence.size()
* @param sentence is a given String sentence.
* @return true if the given sentence is a palindrome.
*/
public static boolean isPalindrome(String sentence) {
sentence = sentence.replaceAll("[^a-zA-Z0-9]","").toLowerCase();
char[] sentenceChars = sentence.toCharArray();
for (int i = 0; i < sentenceChars.length / 2; i++) {
if (sentenceChars[i] != sentenceChars[sentenceChars.length - 1 - i]) {
return false;
}
}
return true;
}
使用short,long和错误参数运行它会为您提供正确的值...
public static void main(String[] args) {
String wrong = "ABCA";
System.out.println("Is '" + wrong + "' a palindrome? ");
System.out.println(isPalindrome(wrong));
String none = "A'";
System.out.println("Is '" + none + "' a palindrome? ");
System.out.print(isPalindrome(none));
String a = "Madam, I'm Adam";
System.out.println("Is '" + a + "' a palindrome? ");
System.out.println(isPalindrome(a));
String b = "abba";
System.out.println("Is '" + b + "' a palindrome? ");
System.out.println(isPalindrome(b));
String toyota = "A Toyota. Race fast, safe car. A Toyota.";
System.out.println("Is '" + toyota + "' a palindrome? ");
System.out.println(isPalindrome(toyota));
String longestPalindrome = "Do good? I? No! Evil anon I deliver. I maim " +
"nine more hero-men in Saginaw, sanitary sword a-tuck, Carol, I -- lo! " +
"-- rack, cut a drowsy rat in Aswan. I gas nine more hero-men in Miami. " +
"Reviled, I (Nona) live on. I do, O God!";
System.out.println("Is '" + longestPalindrome + "' a palindrome? ");
System.out.println(isPalindrome(longestPalindrome));
}
}
这是执行的输出......
Is 'ABCA' a palindrome? false
Is 'a' a palindrome? true
Is 'Madam, I'm Adam a palindrome?' true
Is 'abba a palindrome? true'
Is 'A Toyota. Race fast, safe car. A Toyota.' a palindrome? true
Is 'Do good? I? No! Evil anon I deliver. I maim nine more hero-men in Saginaw, sanitary sword a-tuck, Carol, I -- lo! -- rack, cut a drowsy rat in Aswan. I gas nine more hero-men in Miami. Reviled, I (Nona) live on. I do, O God!' a palindrome? true