处理有关Java的面试问题,直到....回文问题为止,一切都很好。
我决定使用递归方法,并且正确编写了代码(或者我以为自己做了!)。但是,我收到了无效的方法消息,并且我不明白为什么。
我完全感到困惑吗?
其次,我发现此解决方案颇费周折,所以我想知道是否还有另一种较短的递归方法容易记住(我知道您可以使用字符串缓冲区,但我读到它不很健壮才是真的。在面试压力下,这会不会很重要?
这是代码。我期待您的答复:
public class HowToCheckForAPalindromeString {
//Logic to solve this problem: If the characters at the beginning and the end are the same = Palindrome
//Method : Using Recursion: in which a method calls itself to solve some problem.
public static void main(String[] args) {
String pal = "Rotator";
boolean result = isPalindrome(pal);
System.out.println(pal + "is Palindrome ="+result);
//Body of the Palindrome Method
public static boolean isPalindrome(String pal){
//step 1. Check for exception handling for edge cases
if(pal==null){
return false; //check if the beginning and the of the palindrome are equal
}
if(pal.length()<=1){
return true; //if a person enters an empty string that's a palindrome (very important!)
}
//step 2. Finding the Palindrome
String firstLet = pal.substring(0, 1); //this will check the first characters of the string
String lastLet = pal.substring(pal.length()-1, pal.length());// check the starting and the ending index
if(!firstLet.equals(lastLet)){//if the the first and last letters are NOT the same = not palindome
return false;
}else{
return isPalindrome(pal.length()-1, pal.length()); //if they are the same= Palimdrome- Yay!
}
}//end of method
}
}
答案 0 :(得分:1)
在递归调用中,您尝试传递2个整数,该整数应为字符串。假设您要传递剪切第一个和最后一个字母的子字符串,这是固定代码:
public class HowToCheckForAPalindromeString {
//Logic to solve this problem: If the characters at the beginning and the end are the same = Palindrome
//Method : Using Recursion: in which a method calls itself to solve some problem.
public static void main(String[] args) {
String pal = "Rotator";
boolean result = isPalindrome(pal);
System.out.println(pal + " is Palindrome = " + result);
}
//Body of the Palindrome Method
public static boolean isPalindrome(String pal){
//step 1. Check for exception handling for edge cases
if(pal==null){
return false; //check if the beginning and the of the palindrome are equal
}
if(pal.length()<=1){
return true; //if a person enters an empty string that's a palindrome (very important!)
}
//step 2. Finding the Palindrome
char firstLet = pal.charAt(0);
char lastLet = pal.charAt(pal.length()-1);//simpler way to get first and last chars
if(!firstLet.equals(lastLet)){//if the the first and last letters are NOT the same = not palindome
return false;
}else{
return isPalindrome(pal.substring(1,pal.length()-1)); //this will cut first and last letter
}
}//end of method
}//end of class
如您所见,您需要从索引1(删除索引0处的字母)到索引长度1(在substring
方法中是唯一的,因此它将删除最后一个字母)的子字符串。
答案 1 :(得分:0)
嗨,我能够自己修改代码。我注意到我错过了重要但很少的信息。我将发布代码给其他可能处于同一位置的人。
使用递归方法对回文进行关键处理:
该类中有2个方法。 1用于输出,另一用于 找到回文
请小心,因为您可能会遗漏代码信息或将其放置在错误的位置。很难发现并修复。
String pal = "rotator";
boolean result = isPalindrome(pal);
System.out.println(pal + " is Palindrome ="+result);
}
//End of method1
//Body of the Palindrome Method
//方法2:使用递归
公共静态布尔isPalindrome(字符串pal){
//step 1. Check for exception handling and edge cases
if(pal==null){
return false; //check if the beginning and the end of the palindrome are same
}
if(pal.length()<=1){
return true; //if a person enters an empty string that's a palindrome (very important!)
}
//step 2. Finding the Palindrome
String firstLet= pal.substring(0,1);
String lastLet = pal.substring(pal.length()-1, pal.length());
if(!firstLet.equals(lastLet)){//if the the first and last letters are NOT the same = not palindome
return false;
}else{
return isPalindrome(pal.substring(1, pal.length()-1)) ; //first and last letter of string are same
}//end of if loop
}//end of recursive method