2012-09-27 49 views
3

以下是我用於方法lastIndexOfch的字符是匹配的字符,並且str是源字符串。java遞歸查找字符串中字符的最後一個索引

public static int lastIndexOf(char ch, String str) { 
    // check for null string or empty string 
    if (str.length() == 0 || str == null) { 
     return -1; 
    } 

    int indexInRest = lastIndexOf(ch, str.substring(1)); 
    char first = str.charAt(0); 

    // recursive call to find the last matching character 
    if (first == ch) { 
     return 1 + indexInRest; // this might not work properly 
    } else 
     return indexInRest; 
} 

如果在我的課的主要方法,我稱之爲:

System.out.println(lastIndexOf('r', "recurse")); 
    System.out.println(lastIndexOf('p', "recurse")); 

我:

1 
-1 

期望的結果是:

4 
-1 

建議,請。

+0

是這個家庭作業? – user1329572

+1

以及現有方法有什麼問題http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#lastIndexOf%28int%29? –

+0

是的,我需要建議。 – Hank

回答

3

如何服用功能方法..

public static int lastIndexOf(char ch, String str) { 
    if (str.charAt(str.length() - 1) == ch) { return str.length() -1; } 
    if (str.length() <= 1) { return -1; } 
    return lastIndexOf(ch, str.substring(0, str.length() - 1)); 
} 
+0

這就是我需要的。謝謝約翰。 – Hank

0

爲什麼不使用String.lastIndexOf這樣的:

str.lastIndexOf(ch) 
+5

可能是因爲它的功課和老師要求重新發明這個輪子。 –

0

使用String#lastIndexOf(int ch)實現作爲一般準則,

public int lastIndexOf(int ch) { 
    return lastIndexOf(ch, value.length - 1); 
} 

public int lastIndexOf(int ch, int fromIndex) { 
    if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 
     // handle most cases here (ch is a BMP code point or a 
     // negative value (invalid code point)) 
     final char[] value = this.value; 
     int i = Math.min(fromIndex, value.length - 1); 
     for (; i >= 0; i--) { 
      if (value[i] == ch) { 
       return i; 
      } 
     } 
     return -1; 
    } else { 
     return lastIndexOfSupplementary(ch, fromIndex); 
    } 
} 

private int lastIndexOfSupplementary(int ch, int fromIndex) { 
    if (Character.isValidCodePoint(ch)) { 
     final char[] value = this.value; 
     char hi = Character.highSurrogate(ch); 
     char lo = Character.lowSurrogate(ch); 
     int i = Math.min(fromIndex, value.length - 2); 
     for (; i >= 0; i--) { 
      if (value[i] == hi && value[i + 1] == lo) { 
       return i; 
      } 
     } 
    } 
    return -1; 
} 

而這一點,

lastIndexOf(ch, value.length - 1); 

value是目標String作爲一個字符數組。

+0

重要的是要注意Java使用迭代方法......您需要將其轉換爲遞歸方法。 – user1329572

0

首先,你應該更改爲:

if (str == null || str.length() == 0) { 

因爲NPE會提高,如果strnull

添加deep paramater你這樣的代碼:

public static int lastIndexOf(char ch, String str, int deep) { 

,並增加其價值每遞歸調用

int indexInRest = lastIndexOf(ch, str.substring(1), deep++); 

然後,在回一句,加入深到你的返回值:

return 1 + indexInRest + deep; // this might not work properly 

調用該函數第一次與deep = 0,或者更好,使這兩個參數的方法lastIndexOf調用3個參數版本lastIndexOfdeep參數設置爲0

3

這一定是因爲功課就沒有點寫這個方法,因爲String.lastIndexOf()API中的存在,並使用遞歸這樣做將是緩慢和使用大量的記憶。

這裏有一個提示。現在你的算法正在從前面剔除字符(substring(1))並比較它們。 lastIndexOf()應該從刪除字符串後面的字符開始尋找匹配項,然後在找到匹配項時退出。

0

您還可以使用Matcher以便預測字符串分析通過你的作業要求的演變:

public int getlastMatch(String searchPattern,String textString) { 
     int index = -1; 
     Pattern pattern = Pattern.compile(searchPattern); 
     Matcher matcher = pattern.matcher(textString); 

     while(matcher.find()) { 
       index = matcher.start(); 
     } 
     return index; 
    } 

其中textString可能是你的性格有關。

因此返回字符串內部分字符串的最後一次出現。

相關問題