【问题标题】:java recursion find the last index of a character in a stringjava递归查找字符串中字符的最后一个索引
【发布时间】:2012-09-19 09:57:44
【问题描述】:

这是我对方法 lastIndexOf 的内容,ch 是要匹配的字符,str 是源字符串。

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

请给个建议。

【问题讨论】:

  • 现有方法docs.oracle.com/javase/6/docs/api/java/lang/…有什么问题?
  • 是的,我需要建议。
  • 您搜索的是从第一个字符到最后一个字符,而不是从最后一个字符到第一个字符。此外,您的空值检查应该是第一个,长度检查是第二个。
  • int lastIndexOf(int char) docs.oracle.com/javase/6/docs/api/java/lang/…> 采用 unicode 字符值。因为这是递归的作业。我绝对不能用那种方法。

标签: java string recursion


【解决方案1】:

为什么不像这样使用String.lastIndexOf

str.lastIndexOf(ch)

【讨论】:

  • 可能是因为这是家庭作业,老师要求重新发明这个轮子。
【解决方案2】:

使用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 是作为字符数组的目标字符串。

【讨论】:

  • 请务必注意,Java 使用迭代方法...您需要将其转换为递归方法。
【解决方案3】:

首先,你应该改为:

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

因为如果strnull,NPE 可能会引发

deep 参数添加到您的代码中,如下所示:

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

并在每次递归调用时增加其值

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

然后,在返回语句中,为你的返回值添加 deep:

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

第一次调用该函数时使用deep = 0,或者更好的是,使lastIndexOf的二参数方法调用lastIndexOf的三参数版本,并将deep参数设置为0

【讨论】:

    【解决方案4】:

    这一定是功课,因为 API 中存在 String.lastIndexOf(),因此编写此方法没有意义,并且使用递归执行此操作会很慢并且会占用大量内存。

    这里有个提示。现在,您的算法正在从前面切掉字符( substring(1) )并比较它们。 lastIndexOf() 应该首先删除字符串后面的字符以寻找匹配项,然后在找到匹配项时退出。

    【讨论】:

      【解决方案5】:

      您还可以使用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 可能是你关心的角色。

      因此返回字符串中最后一次出现的部分字符串。

      【讨论】:

        【解决方案6】:

        采用功能方法怎么样..

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

        【讨论】:

        • 这就是我需要的。谢谢约翰。
        猜你喜欢
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 2013-05-06
        • 2015-12-15
        相关资源
        最近更新 更多