對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置(從0開始)。如果不存在,則返回 -1。說明
在面試中我是否需要實現kmp演算法?
樣例
如果 source = 「source」 和 target = 「target」,返回 -1。
如果 source = 「abcdabcdefg」 和 target = 「bcd」,返回 1。
根據說明可以知道本題是實現查詢子串,於是我用的bf演算法。
class solution else
if(count == strlen(target)) return i - j;
}return -1;
}};
去網上查了下kmp演算法做的,bf是o(n2)的演算法,而kmp是o(n)的演算法kmp做法
LintCode 13 字串查詢
對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置 從0開始 如果不存在,則返回 1。如果 source source 和 target target 返回 1。如果 source abcdabcdefg 和 targ...
LintCode 13 字串查詢
題目描述 對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置 從0開始 如果不存在,則返回 1。說明 在面試中我是否需要實現kmp演算法?不需要,當這種問題出現在面試中時,面試官很可能只是想要測試一下你的基礎應用能力。...
lintcode 13 字串查詢
字串查詢 中文english 對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置 從0開始 如果不存在,則返回 1。樣例 樣例 1 輸入 source source target target 輸出 1 樣例解釋 如果...