我正在嘗試使用 O(N) 復雜度在字串中找到子字串。下面是我寫的代碼。它回傳未定義,我不知道為什么。請讓我知道我的代碼出了什么問題。
let omg = "omg";
let omgi = "omjiklmonomgib";
function stringSearch(smallString, bigString) {
let left = 0;
let right = left (smallString.length - 1);
while (left > right) {
if (smallString[left] === bigString[left]) {
if (smallString[right] === bigString[right]) {
left ;
right--;
if (left === right) {
if (smallString[right] === bigString[left]) {
return true;
} else if (right === left 1) {
if (smallString[right] === bigString[right] && smallString[left] === bigString[left]) {
return true;
} else {
left = right 1;
right = left (smallString.length - 1);
}
}
}
}
}
}
}
console.log(stringSearch(omg, omgi)); //Undefined
uj5u.com熱心網友回復:
據我了解,您只是在重新制作String.prototype.match。嘗試檢查一下,因為這可能是執行您所說的更簡單的方法。抱歉,錯過了 O(N) 復雜度部分。
如果你真的想定制一個,我可以給你一些建議。
首先,您應該有一個“快取”變數(一個為所有人,而不是leftand right)和另一個變數found(它將是一個布林值,因此將其設定為 false)。“快取”變數將存盤文本,另一個將存盤您是否找到了smallString.
基本上,您遍歷每個字符并將長smallString字符存盤在“快取”變數中。一旦“快取”變數的長度與 相同smallString,就在其上運行 if 陳述句。如果它不等于 ,則smallString洗掉“快取”的第一個字符。回圈中的下一次迭代,它將添加另一個字符。然后你和以前一樣,運行一個 if 陳述句,如果它不相等,洗掉第一個字符并繼續回圈,直到找到它,或者字串結束。如果找到,請將布林值設定為 true。
像這樣的東西:
function stringSearch(smallString, bigString, caseSensitive=true) {
if(!caseSensitive) { // if caseSensitive is false, make everything lower case
smallString = smallString.toLowerCase();
bigString = bigString.toLowerCase();
}
let cache = ""; // string cache
let found = false; // result
for(i=0;i<bigString.length;i ) { // loop through every character in bigString
cache =bigString[i]; // add the current character to the cache
if(cache.length == smallString.length) { // check if the cache's length is the same as the smallString's length
if(cache == smallString) { // check if the cache is equal to the smallString
found = true; // set found to true
break; // break the loop (stop it from going on)
} else {
cache = cache.substring(1); // set cache to itself but remove the first character
}
}
}
return found; // return result
}
// example:
console.log("String 'hello, world' has 'hello': " stringSearch("hello", "hello, world"));
console.log("String 'HELLO WORLD' has 'hello': " stringSearch("hello", "HELLO WORLD"));
console.log("String 'HELLO WORLD' has 'hello' (not case sensitive): " stringSearch("hello", "HELLO WORLD", false));
console.log("String 'hey hi hello WORLD' has 'hello': " stringSearch("hello", "hey hi hello WORLD"));
console.log("String 'hey hi hello' has 'hello': " stringSearch("hello", "hey hi hello"));
uj5u.com熱心網友回復:
我看到了幾個問題。首先,代碼沒有針對每個邏輯分支的 return 陳述句。我的意思是,對于 if、then、else 陳述句中的每個條件,代碼都應該有一個 return 陳述句或某種控制流陳述句(例如遞回呼叫或 continue),最終會導致回傳陳述句。
第二個問題是while回圈。假設,right應該大于left從函式的開頭(除非長度smallString為 0),因為right是smallerString負 1的長度。while條件是left > right,所以 while 內的任何內容都不會被執行,除非smallString有負長度(它沒有't)。
順便說一句,如果您想檢查整個bigString,則需要迭代bigString,而不是smallString。如果您只檢查smallString.length字符,則不會檢查整個bigString. 弄清楚如何撰寫這個函式是一個很好的練習,所以我將把它的撰寫留給你,不要自己提供實作。保持良好的作業!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388741.html
標籤:javascript 数组 细绳 算法 子串
下一篇:如何根據第一個空格拆分R中的列
