为什么JDK中String类的indexof不使用KMP或者Boyer-Moore等时间复杂度低的算法编辑器

今天在leetcode上刷题,正好刷到查找字符串的题目,想到了以前了解的KMP和Boyer-Moore等算法。这两个及其类似的算法的时间复杂度都接近于O(n)。

后面自己又去看了下JDK的String类中的indexof方法的实现,发现很奇怪,仅仅只是用了暴力破解法,也就是最原始的实现,时间复杂度也到了O(n*m)。

String类的indexof(String s)方法中调用一下方法:


/**
     * Code shared by String and StringBuffer to do searches. The
     * source is the character array being searched, and the target
     * is the string being searched for.
     *
     * @param   source       the characters being searched.
     * @param   sourceOffset offset of the source string.
     * @param   sourceCount  count of the source string.
     * @param   target       the characters being searched for.
     * @param   targetOffset offset of the target string.
     * @param   targetCount  count of the target string.
     * @param   fromIndex    the index to begin searching from.
     */
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j]
                        == target[k]; j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
    }

谷歌并翻了下StackOverflow:

原来JDK的编写者们认为大多数情况下,字符串都不长,使用原始实现可能代价更低。因为KMP和Boyer-Moore算法都需要预先计算处理来获得辅助数组,需要一定的时间和空间,这可能在短字符串查找中相比较原始实现耗费更大的代价。而且一般大字符串查找时,程序员们也会使用其它特定的数据结构,查找起来更简单。这有点类似于排除特定情况下的快速排序了。不同环境选择不同算法。

Reference:

http://stackoverflow.com/questions/19543547/why-jdks-string-indexof-does-not-use-kmp/

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
JDK 8 之前,Java 时间类主要有两个:`java.util.Date` 和 `java.util.Calendar`。 `java.util.Date` 类是用来表示日期和时间的类。它包含了一些常用的方法,比如获取当前时间、设置时间、比较时间等。然而,`java.util.Date` 类有一些问题,比如它的设计不够好,不支持时区、线程安全性差等。 `java.util.Calendar` 类是一个抽象类,用于处理日期和时间的各种操作。它提供了许多方法来获取和设置年、月、日、时、分、秒等信息,并且支持时区设置。使用 `java.util.Calendar` 类可以进行日期的加减运算、格式化输出等操作。 下面是一个简单的示例代码,演示了如何使用 `java.util.Date` 和 `java.util.Calendar` 类: ```java import java.util.Date; import java.util.Calendar; public class Main { public static void main(String[] args) { // 使用 java.util.Date 类 Date date = new Date(); System.out.println("当前时间:" + date); // 使用 java.util.Calendar 类 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; // 注意月份是从 0 开始的,所以要加 1 int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.println("当前时间:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second); } } ``` 总的来说,JDK 8 之前的时间使用起来比较繁琐,并且存在一些问题。在 JDK 8 之后,引入了新的日期和时间 API(即 `java.time` 包),提供了更好的时间处理方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值