Java 使用反向方法分离带out的字符串
数组包含回文字符串和其他字符串。使用字符串反转方法分离不带out的回文字符串?Java 使用反向方法分离带out的字符串,java,string,Java,String,数组包含回文字符串和其他字符串。使用字符串反转方法分离不带out的回文字符串?公共布尔值isAlindrome(字符串pal){ public boolean isPalindrome(String pal) { if (pal.length() <= 1) { return true; // BASE CASE. } // Get the first and last characters of the String. c
公共布尔值isAlindrome(字符串pal){
public boolean isPalindrome(String pal) {
if (pal.length() <= 1) {
return true; // BASE CASE.
}
// Get the first and last characters of the String.
char first = pal.charAt(0);
char last = pal.charAt(pal.length()-1);
if (Character.isLetter(first) && Character.isLetter(last)) {
if (first != last) {
return false; // BASE CASE.
}
else {
Palindrome sub = new Palindrome(
pal.substring(1,pal.length()-1));
return sub.isPalindrome(); // RECURSIVE CASE.
}
}
else if (!Character.isLetter(first)) {
Palindrome sub = new Palindrome(pal.substring(1));
return sub.isPalindrome(); // RECURSIVE CASE.
}
else {
Palindrome sub = new Palindrome(pal.substring(0, pal.length()-1));
return sub.isPalindrome(); // RECURSIVE CASE.
}
}
如果(pal.length()您可以尝试这样做。让SearchString成为包含要搜索的字符串的ArrayList
for (String str : searchStrings) {
char[] strArray = str.toCharArray();
int i = 0;
int j = strArray.length - 1;
while (true) {
if (strArray[i] != strArray[j]) {
System.out.println(str + " is not a palindrome.");
break;
}
if (i == j || i > j) {
System.out.println(str + " is a palindrome.");
break;
}
i++;
j--;
}
}
为什么还要添加这些内容呢?留空也不会有什么不同。你至少可以从标题中删除你老师问的问题的编号。。。