理解递归代码

时间:2016-02-12 18:13:53

标签: java

我需要一些帮助才能理解"幕后花絮#34;我的部分代码。我有一个TA帮我处理部分代码,所以我不完全确定其中的一些代码。

以下是我的问题(我知道你应该分别发布多个问题,但这两个问题齐头并进,总体而言,这是一个非常简短的问题。)

  1. 使用递归方法,如果输入“radar”一词,则写出每一步处理的字符串。为“稻草艺术”做同样的事情。因此,像逐步分解递归方法如何确定雷达和稻草艺术都是回文一样。那么如果我将雷达输入我的程序,它将如何在幕后看?
  2. 然后我只是需要一些帮助,使用简短的迭代方法为回文做同样的事情:

    1. 使用迭代方法(使用循环)编写一些简单的代码来解决同样的问题。 (问题是 - 实现一个使用递归方法的程序,该方法确定用户输入的字符串是否是回文。如果字符串是向前的,它是向后的,如“雷达”或“塔可猫” ”。)
    2. 这是我使用递归方法解决的代码:

      import java.util.Scanner;
      public class RecursivePalindrome {
      
      public static void main(String[] args) 
      {
          // TODO Auto-generated method stub
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Enter a word and I will determine if it is a palindrome");//Gets user input
          String word = keyboard.nextLine();
          String combined = word.replaceAll("[\\W]", "");//matches non-word characters, and removes the spaces.
          combined = combined.toLowerCase();//makes the combined word all lowercase
          if(checkPalindrome(word))//calls palindrome method and sees if combined word is a palindrome
          {
              System.out.println("The word " +  word +  " is a palindrome");
          }
          else
          {
              System.out.println("The word " + word + " is not a palindrome");
          }
      }
      
      private static boolean checkPalindrome(String word) 
      {
          if(word.length() <= 1)//if length is less than or equal to 1 then the string is palindrome
          {
               return true;
          } 
          if(word.charAt(0) == word.charAt(word.length()-1))//checks the first and last char of the string
          {
              //if true, then does the same thing with substring
              //returns only when the string is done checking
              return checkPalindrome(word.substring(1, word.length()-1));
          }
          //if string doesn't pass check, it's not a palindrome
          return false;
      }
      }
      

2 个答案:

答案 0 :(得分:0)

你的第一个问题可能看起来很复杂,但它的目的是在你的递归方法中放置一些很好的System.out.println()指令。

预期的学习效果是,你可以看到那里实际发生的事情。

只需在方法的开头放置System.out.println(word)即可。然后在每个if语句之后使用更多System.out.println()和符合条件的语句来执行此操作。

要迭代检查,你只需检查第一个字母是否等于最后一个字母,第二个字母等于后面的第二个字母,依此类推:

public static boolean isPalindrom(String word){
    for(int i = 0; i < word.length() / 2; i++) {
        if(word.charAt(i) != word.charAt(word.length()-1-i)) {
            return false;
        }
    }
    return true;
}

答案 1 :(得分:0)

问题2),循环方法

private static boolean checkPalindrome(String word) 
{
    int ii = 0;
    int io = word.length()-1;

    while(ii<io)
    { 
        if((word.charAt(ii) == ' ') ii++;
        else if((word.charAt(io) == ' ') io--;
        else if(Character.toLowerCase(word.charAt(ii))
            != Character .toLowerCase(word.charAt(io))) return false;
        else { ii++; io--; }
    }
    return true;
}

递归电话:

boolean result = checkPalindrome(word, 0, word.lenght()-1);

proc

private static boolean checkPalindrome(String word, int ii, int io) 
{   
    while((ii<io)&&(word.charAt(ii) == ' ')) ii++; // skipp spaces
    while((io>ii)&&(word.charAt(io) == ' ')) io--; // skipp spaces
    if(ii>=io) return true;
    if(Character.toLowerCase(word.charAt(ii))
        == Character .toLowerCase(word.charAt(io))) 
            return checkPalindrome(word, ii++, io--);
    return false;
}