Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不返回字符串的递归方法_Java_Palindrome - Fatal编程技术网

Java 不返回字符串的递归方法

Java 不返回字符串的递归方法,java,palindrome,Java,Palindrome,我必须创建一个代码,可以找到句子中包含的最长回文。(有些人喜欢蛋糕,但我更喜欢馅饼;最长的回文是我更喜欢圆周率)。问题是,在运行代码时,它不会返回回文。我不确定问题出在哪里,但如果有人能解决这个问题,我希望你能告诉我。谢谢 代码如下 public class Recursion6 { static String recursion(String word, int currentLength, int x, String substring) { String

我必须创建一个代码,可以找到句子中包含的最长回文。(有些人喜欢蛋糕,但我更喜欢馅饼;最长的回文是我更喜欢圆周率)。问题是,在运行代码时,它不会返回回文。我不确定问题出在哪里,但如果有人能解决这个问题,我希望你能告诉我。谢谢

代码如下

public class Recursion6 {
    
    static String recursion(String word, int currentLength, int x, String substring) {
        String reverse =new StringBuffer(word).reverse().toString();
        if(word.length() == 1 ){
            return substring;
        }
        if(word.charAt(0) != word.charAt(x)) {
            if(x == word.length() - 1) {
                recursion(word.substring(1), currentLength, 1, substring);
            }
            x++;
            recursion(word, currentLength, x, substring);
        } else {
            if(word.substring(0, x + 1).equalsIgnoreCase(reverse.substring(word.length() - (x+1), word.length()))) {
                if(word.substring(0, x).length() > currentLength) {
                    currentLength = word.substring(0, x + 1).length();
                    substring = word.substring(0, x + 1);
                    
                }
                recursion(word.substring(1), currentLength, 1, substring);
            }
            recursion(word.substring(1), currentLength, 1, substring);
        }
        return substring;
    }
    

    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
    System.out.println("Enter a Sentence:");
    String word=sc.nextLine();
        System.out.println("The Palendrome is "+recursion(word.replaceAll(" ", ""), 1, 1, null));        
    sc.close();
    }
}

忽略返回值

例如:

       recursion(word, currentLength, x, substring);
有一个返回值,好的。您只需对递归调用不做任何处理。从最外层调用返回的只是最外层调用的输入,它是一个空字符串


您可能需要回顾递归激活是如何工作的。“return”语句只从当前级别返回,它不会清空整个调用堆栈。

这里是另一种方法

  • 首先,只需获取堆栈上的所有子字符串。这就是前两个递归调用所做的
  • 然后在堆栈展开时检查每个子字符串,看看它是否是回文
  • 私有方法执行实际检查。它只是将字符的两端朝中间比较
  • 最终输出可能包含空格和/或标点符号
印刷品

' a man, a plan, a canal, panama!, '
该方法获取字符串、起始索引和结束索引

public static String findLongestPalindrome(String s, int start, int end) {
    String save = "";
    if (end - start > 0) {
        save = findLongestPalindrome(s, start + 1, end);
    } else if (end > 1) {
        save = findLongestPalindrome(s, 0, end - 1);
    }
    String temp;
    if (isPalindrome(temp = s.substring(start, end))) {
        if (temp.length() > save.length()) {
            save = temp;
        }
    }
    return save;
}
    
// iterates from the ends toward the middle.
    
private static boolean isPalindrome(String s) {
    s = s.replaceAll("\\W+", "").toLowerCase();
    int end = s.length();
    for (int i = 0; i < s.length() >> 1; i++) {
        if (s.charAt(i) != s.charAt(--end)) {
            return false; // fast fail if any two are not equal
        }
    }
    return true;
}
公共静态字符串findLongestPalindrome(字符串s,int-start,int-end){
字符串save=“”;
如果(结束-开始>0){
save=findlongest回文(s,开始+1,结束);
}否则如果(结束>1){
save=findlongest回文组(s,0,end-1);
}
字符串温度;
if(isAlindrome(temp=s.substring(开始,结束))){
如果(临时长度()>保存长度()){
保存=温度;
}
}
返回保存;
}
//从末端向中间迭代。
私有静态布尔值isAlindrome(字符串s){
s=s.replaceAll(“\\W+”,“”)。toLowerCase();
int end=s.length();
对于(int i=0;i>1;i++){
如果(s.charAt(i)!=s.charAt(--end)){
return false;//如果任意两个不相等,则快速失败
}
}
返回true;
}

最长回文子字符串,这就是您要问的吗?最长的明文包含在子字符串变量中。这个想法是,当程序搜索完句子后,它将返回子字符串并打印出来。我试着用if(word.length()==1)来做这件事,但是它没有返回任何与这个问题相同的问题:,也许你从那里的答案中学到了一些东西,然后可以修复你的程序。这对你有用吗?如果没有,请解释。
public static String findLongestPalindrome(String s, int start, int end) {
    String save = "";
    if (end - start > 0) {
        save = findLongestPalindrome(s, start + 1, end);
    } else if (end > 1) {
        save = findLongestPalindrome(s, 0, end - 1);
    }
    String temp;
    if (isPalindrome(temp = s.substring(start, end))) {
        if (temp.length() > save.length()) {
            save = temp;
        }
    }
    return save;
}
    
// iterates from the ends toward the middle.
    
private static boolean isPalindrome(String s) {
    s = s.replaceAll("\\W+", "").toLowerCase();
    int end = s.length();
    for (int i = 0; i < s.length() >> 1; i++) {
        if (s.charAt(i) != s.charAt(--end)) {
            return false; // fast fail if any two are not equal
        }
    }
    return true;
}