简体   繁体   English

使用递归来检查字符串是否是回文?

[英]To check if string is palindrome using recursion?

There is something wrong with my code as one the testcase in my assignment is coming out wrong, giving me runtime error when I submit the code online. 我的代码有问题,因为我的任务中的测试用例出错了,当我在线提交代码时给出了运行时错误。 That testcase could be any String. 该测试用例可以是任何String。 I believe that everything is fine with the code as I have checked it manually for many testcases. 我相信代码的一切都很好,因为我已经手动检查了许多测试用例。

HERE IS THE CODE 这是代码

public static boolean isStringPalindrome(String input) {
    if(input.length()==0 || input.length()==1)
        return true;

    int first = 0;
    int last = input.length()-1;

    if(input.charAt(first) != input.charAt(last))
        return false;

    String str="";
    for(int i=first+1;i<last;i++){
        str = str+input.charAt(i); 
    }
    boolean sa = isStringPalindrome(str);
    return sa; 
}

Sample Input 样本输入

racecar

Output 产量

true   

Sample Input 样本输入

pablo

Output 产量

false   

Your code appears to be overly complicated for recursively testing if the String is a palindrome. 如果String是回文,你的代码似乎过于复杂,无法递归测试。 Something like, 就像是,

public static boolean isStringPalindrome(String input) {
    if (input == null) {
        return false;
    } else if (input.isEmpty() || input.length() == 1) {
        return true;
    }
    int len = input.length() - 1;
    return input.charAt(0) == input.charAt(len) //
            && isStringPalindrome(input.substring(1, len));
}

Is recursive without embedding a for loop. 在没有嵌入for循环的情况下是递归的。 Because if you can do that, you should do something like 因为如果你能做到这一点,你应该做点什么

public static boolean isStringPalindrome(String input) {
    if (input == null) {
        return false;
    } else if (input.isEmpty() || input.length() == 1) {
        return true;
    }
    int len = input.length();
    for (int i = 0; i <= len / 2; i++) {
        if (input.charAt(i) != input.charAt(len - 1 - i)) {
            return false;
        }
    }
    return true;
}

A simpler way to check for palindrome can be: 检查回文的简单方法可以是:

public static boolean isPalindrome(String s)
{   if (input == null)
        return false;
    else if(s.length() == 0 || s.length() == 1)
        return true;

    /* check for first and last char of String:
     * if they are same then do the same thing for a substring
     * with first and last char removed. and carry on this
     * until you string completes or condition fails.
     */
    if(s.charAt(0) == s.charAt(s.length()-1))
        return isPalindrome(s.substring(1, s.length()-1));

    return false;
}

Update 更新

You are getting runtime error(NZEC) which means non-zero exit code . 您将获得runtime error(NZEC) ,这意味着non-zero exit code It means your program is ending unexpectedly. 这意味着您的程序意外结束。 I don't see any reason except that your program doesn't have a null check. 我没有看到任何理由,除了你的程序没有null检查。 Otherwise, I have gone through your code carefully, you are doing the same thing which I have suggested. 否则,我仔细检查了你的代码,你正在做我建议的同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM