leetcode 30 串聯所有單詞的子串

2022-06-20 12:39:12 字數 1446 閱讀 5141

串聯所有單詞的子串

解題思路:滑動視窗以及hash

class

solution

int slen =s.length();

int wordlen =words.length;

int itemlen = words[0].length();

integer t = null

; string temps = null

;

int n = 0;

mapmap = new hashmap<>();

mapmap2 = new hashmap<>();

string strs = new string[slen-itemlen+1];

for(int i=0;i)

else

}for(int i=0;i)

}for(int i=0;i)

else

map2.put(temps,t+1);

}n++;

if(n==wordlen)

temps = strs[i+n*itemlen];}}

return

result;

}}

第二種方法:

解題思路:在第一種基礎上,不清空hash表,而是逐漸調整hash表,這樣子會節省大量的時間。

class

solution

int slen =s.length();

int wordlen =words.length;

int itemlen = words[0].length();

integer t = null

; string temps = null

;

int n = 0;

int start = 0;

int index = 0;

mapmap = new hashmap<>();

mapmap2 = new hashmap<>();

string strs = new string[slen-itemlen+1];

for(int i=0;i)

else

}for(int i=0;i)

}for(int i=0;i)

else

else

t =map2.get(strs[index]);

map2.put(strs[index],t-1);

start +=itemlen;

n--;

}t =map2.get(strs[index]);

map2.put(strs[index],t+1);

}n++;

if(n==wordlen)}}

}return

result;

}}

leetcode 30 串聯所有單詞的子串

leetcode題目鏈結 題目要求 找出 由words陣列組成的字串 每乙個元素word等長 在字元轉s中的位置 陣列words生成的字典dic2 遍歷字串,從頭開始判斷長度為lenwords的字串 生成的字典dic1 如果dic1 與 dic2 相同,說明找到 def findsubstring ...

leetcode 30 串聯所有單詞的子串

給定乙個字串 s 和一些長度相同的單詞 words。找出 s 中恰好可以由 words 中所有單詞串聯形成的子串的起始位置。注意子串要與 words 中的單詞完全匹配,中間不能有其他字元,但不需要考慮 words 中單詞串聯的順序。示例 1 輸入 s barfoothefoobarman words...

leetcode 30 串聯所有單詞的子串

題目 給定乙個字串 s 和一些長度相同的單詞 words。找出 s 中恰好可以由 words 中所有單詞串聯形成的子串的起始位置。注意子串要與 words 中的單詞完全匹配,中間不能有其他字元,但不需要考慮 words 中單詞串聯的順序。示例 1 輸入 s barfoothefoobarman wo...