2023-01-03
朴素算法
public static int searchKeyIndex(String str, String key) { int strLength = str.length(); int keyLength = key.length(); for (int i = 0; i <= strLength - keyLength; i++) { int j; for (j = 0; j < keyLength; j++) { if (key.charAt(j) != str.charAt(i + j)) break; } // key 全都匹配了 if (j == keyLength) return i; } // str 中不存在 key 子串 return -1; }