leetcode 之 Substring with Concatenation of All Words,leetcode
Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
思路:題目的意思是找到包含L中所有單詞的起始位置,但是不能多,也不能少,要剛好包含L中的全部單詞,而且相鄰的兩個開始位置還可以重疊,比如:S="aaa",L=['a','a'],結果是[0,1]。具體方法和最短摘要有點類似,用need數組儲存需要的所有單詞,hasFind儲存已經找到的單詞,如果hasFind大於need,則該位置不滿足,只有當兩個數組完全相同時才滿足。
class Solution {public: vector<int> findSubstring(string S, vector<string> &L) { int totalWord = L.size(),wordLen = L[0].size(),i,j; map<string,int> needWord; vector<int> res; for (i = 0;i < totalWord;i++)needWord[L[i]]++;//統計需要的單詞 for(i = 0;i + totalWord*wordLen <= S.size();i++) { map<string,int> hasFindWord; for (j =0;j < totalWord;j++)//對於每一個單詞 { string sub = S.substr(i+j*wordLen,wordLen);//提取每一個可能的單詞 map<string,int>::iterator iter = needWord.find(sub); if(iter == needWord.end())break; hasFindWord[sub]++; if(hasFindWord[sub] > needWord[sub])break; } if(j == totalWord)res.push_back(i); } return res; }};
階梯英語的POPODOO美語好不好?與新東方英語比,哪個更適合7歲的孩子學?有沒有便宜的教材?
新東方主要是要找到好老師。服務比較完善,興趣激發也很好,價錢比較合理。
新概念英語第三冊摘要寫作全部 網址也行 或發郵件至545174359@qqcom 謝了
到書店買本教師用 新概念 上面應該都有的