2015-04-26 23 views
0

編寫一個叫做isReverse遞歸方法(「字詞1」,「單詞2」)接受兩個字符串作爲參數,如果兩個字符串包含 相同的字符序列對方,但在返回true相反的順序,忽略大小寫,否則返回false。 例如,致電:有什麼不對我的方法isReverse

isReverse("Desserts", "Stressed") 

將返回true。 [所以吃甜食時,你是否有壓力?] 空,一個字母字符串也返回true(如果兩個參數是相同的值)。 這是作業,我很難使這段代碼正常工作。無論我做什麼,它都會返回true。

public static boolean isReverse(String word1, String word2) 
{ 
    if(word1 == null || word2 == null) 
    { 
     if(word1!= null && word2 != null) 
     { 
      return false; 
     } 
     return false; 
    } 
    else if(word1.length() == word2.length()) 
    { 
     String firstWord = word1.substring(0, word1.length()); 
     String secondWord = word2.substring(word2.length()-1); 
     if (firstWord.equalsIgnoreCase(secondWord)) 
     { 
      return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1)); 
     } 
    } 
    return true; 
} 
+0

字符串或StringBuffer類具有反向方法 –

+0

@SrinathGanesh - StringBuilder的具有扭轉,但我們必須假設他沒有被教導有關生成器類。我沒有提到它,我已經度過了3年的大學。 – Hobbyist

+1

鑑於這是作業,我猜測使用內置函數完成95%的作業是禁止的。 – aroth

回答

0

它的工作這是一個鍛鍊?遞歸似乎並不是這裏最好的選擇。無論如何,你只是在修整一個詞,爲什麼?如果您希望比較每個遞歸調用中的每個字符,則必須修剪兩個字。而且你甚至不會將修剪過的單詞作爲參數傳遞給遞歸函數!

你缺少的基本知識是一個基本案例。當遞歸必須返回?在你的情況下,你要減少遞歸每一步的每個字符串的大小,所以你必須有一個基本的例子來檢查大小是否爲1。

希望這個代碼清楚你的頭腦:

public static boolean isReverse(String word1, String word2) { 
    if (word1 == null || word2 == null) { 
     return false; 
    } 
    if (word1.length() == 1 && word2.length() == 1) { 
     //Used equals just for fast compare 
     return word1.equals(word2); 
    } else if (word1.length() == word2.length()) { 
     if (word1.charAt(0) == word2.charAt(word2.length() - 1)) { 
      String firstWord = word1.substring(1, word1.length()); 
      String secondWord = word2.substring(0, word2.length() - 1); 
      System.out.printf("Trimmed %s, %s to %s, %s\n", word1, word2, firstWord, secondWord); 
      return isReverse(firstWord, secondWord); 
     } else { 
      //Characters didn't matched 
      return false; 
     } 
    } else { 
     //Lenght doesn't match 
     return false; 
    } 
} 
1

首先,你有這個設置,以便它只會返回false,如果兩個單詞都爲空;如果它們不爲null,則重新調用方法(如果長度相等),則返回true。

private static boolean isReverse(String a, String b) { 
    // make sure the strings are not null 
    if(a == null || b == null) return false; 

    // If the lengths are not equal, the strings cannot be reversed. 
    if(a.length() != b.length()) { 
     return false; 
    } 

    // Convert string b to an array; 
    char[] bArray = b.toCharArray(); 

    // Create an array to write bArray into in reverse. 
    char[] copy = new char[bArray.length]; 

    // Iterate through bArray in reverse and write to copy[] 
    for(int i = bArray.length; i < 0; i--) { 
     copy[bArray.length - i] = bArray[i]; 
    } 

    // Convert copy[] back into a string. 
    String check = String.valueOf(copy); 

    // See if they reversed string is equal to the original string. 
    if(check.equalsIgnoreCase(a)) { 
     return true; 
    } else { 
     return false; 
    } 
} 
1

你是說

if (firstWord.equalsIgnoreCase(secondWord)) 
    { 
     return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1)); 
    } 

這是確定。但是,如果firstWord不等於第二個詞怎麼辦?

它落下並返回true。

您需要添加一個

else 
    return false; 

我也將增加,你的空檢查將無法正常工作。

if(word1!= null && word2 != null) 
    { 
     return false; 
    } 

沒有用,因爲您已經在if中,只有當word1或word2爲空時纔會發生。所以它們在這裏不能是null和null。

如果您做了它

if(word1 == null && word2 == null) 
    { 
     return true; 
    } 
0

首先,我已經反向的字符串(我把字1)的一個使用recursion.then相比,第二個字符串如果兩個字符串相等,結果設置爲true。

public static boolean isReverse(String word1, String word2) 
{ 
    boolean result = false; 
    //check null to avoid null pointer exception 
    if(word1 == null | word2 == null){ 
     result = false; 
    }else if(word1.length() == word2.length()){ 

     word1 = reverseString(word1); 
     if(word1.equalsIgnoreCase(word2)){ 
      result = true; 
     } 
    } 
    return result; 

} 

static String reverse = ""; 
public static String reverseString(String str){ 

    if(str.length() == 1){ 
     reverse+=str; 

    } else { 
     reverse += str.charAt(str.length()-1) 
       +reverseString(str.substring(0,str.length()-1)); 
    } 
    return reverse; 
} 
+0

你能解釋一下你在這裏做了什麼嗎?僅有代碼的答案往往不是很明顯。 – halfer