Jdk8indexOf時間計算量



Jdk8 Indexof Time Complexity



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

jdk8のソースコードの一部は、アルゴリズムなしで、直接的な暴力の比較を行い、最初に最初の文字を比較し、次に2番目の文字を比較します。時間計算量はO(m * n)です。