【问题标题】:recursion method not returning a string递归方法不返回字符串
【发布时间】:2021-04-21 16:35:39
【问题描述】:

我必须创建一个代码,可以找到句子中包含的最长回文。 (例如,有些人喜欢蛋糕,但我更喜欢馅饼;最长的回文是我更喜欢 pi)。问题是在运行代码时它不会返回回文。我不确定问题是什么,但如果有人能弄清楚,我会很感激你让我知道。谢谢!

代码如下...

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();
    }
}

【问题讨论】:

  • 最长回文子串,是你要问的吗?
  • 最长的plaindrome包含在子字符串变量中。这个想法是,当程序完成搜索句子时,它会返回子字符串并打印它。我尝试使用 if(word.length() == 1) 来执行此操作,但它没有返回任何内容
  • 与此问题相同的问题:Recursive method returning empty value,也许您从那里的答案中学到了一些东西,然后可以修复您的程序。

标签: java palindrome


【解决方案1】:

你忽略了返回值。

例如:

       recursion(word, currentLength, x, substring);

有一个返回值,好吧。你对递归调用什么都不做。最外层调用返回的只是最外层调用的输入,它是一个空字符串。

您可能需要查看递归激活的工作原理。 'return' 语句只从当前级别返回,它不会清空整个调用堆栈。

【讨论】:

    【解决方案2】:

    这是另一种方法。

    • 首先,获取堆栈中的所有子字符串。这就是前两个递归调用的作用。
    • 然后只需在堆栈展开时检查每个子字符串,看看它是否是回文。
    • 私有方法进行实际检查。它只是将两端的字符与中间的字符进行比较。
    • 最终输出可能包含空格和/或标点符号。
    public static void main(String[] args) {
    String s = 
            "Some people like radar, others " +
            "like a man, a plan, a canal, panama!, but " +  
            "my favorite is 'I prefer pie'";
    
    String res = findLongestPalindrome(s,0,s.length());
    System.out.printf("'%s'%n", res);
    

    打印

    ' 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 2020-04-13
      • 2023-03-30
      • 1970-01-01
      • 2019-03-31
      • 2022-01-06
      相关资源
      最近更新 更多