题目:实现strStr()
leetCode链接:实现strStr()
说明:给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。
理解:
1、存在两个字符串haystack和needle,其中字符串needle可能是字符串haystack的子串(可能等于它也可能只是其中的一部分)
2、如果字符串haystack包含了字符串needle,那么返回字符串needle在字符串haystack中第一个匹配字母的下标
3、如果字符串haystack不包含字符串needle,那么返回-1
思路:
1、利用字符串includes Api判断字符串haystack是否包含字符串needle,然后在字符串haystack将字符串needle部分替换成其余字符,最后再利用indexOf Api找出替换后字符的下标即为第一个匹配项的下标
2、直接使用indexOf api
3、暴力匹配
4、Knuth-Morris-Pratt 算法
题解:
方案一:使用字符串includes和indexOf Api
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
if (haystack.includes(needle)) {
const str = haystack.replace(needle, '111')
const index = str.indexOf('111')
return index
}
return -1
};
方案二:indexOf
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
return haystack.indexOf(needle);
};/* */
方案三:暴力匹配
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
let i = 0;
let j = 0;
while (i < haystack.length && j < needle.length) {
if (haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
} else {
//如果不匹配,就回退,从第一次匹配的下一个开始,
i = i - j + 1;
j = 0;
}
if (j == needle.length)
return i - j;
}
return -1;
};
方案四:暴力匹配
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
const n = haystack.length, m = needle.length;
for (let i = 0; i + m <= n; i++) {
let flag = true;
for (let j = 0; j < m; j++) {
if (haystack[i + j] != needle[j]) {
flag = false;
break;
}
}
if (flag) {
return i;
}
}
return -1;
};
方案五:Knuth-Morris-Pratt 算法
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
const n = haystack.length, m = needle.length;
if (m === 0) {
return 0;
}
const pi = new Array(m).fill(0);
for (let i = 1, j = 0; i < m; i++) {
while (j > 0 && needle[i] !== needle[j]) {
j = pi[j - 1];
}
if (needle[i] == needle[j]) {
j++;
}
pi[i] = j;
}
for (let i = 0, j = 0; i < n; i++) {
while (j > 0 && haystack[i] != needle[j]) {
j = pi[j - 1];
}
if (haystack[i] == needle[j]) {
j++;
}
if (j === m) {
return i - m + 1;
}
}
return -1;
};
总结:
本篇提供了几种对strStr的实现,代码有难有易,在实际情况下,我想如果性能差别不大或者即使有一定差距但能接受或者可以从别处弥补的情况下,代码还是越简单越好,这样便于大家理解。