下面是用哈希表写 ,主要看上面那个解法把 classSolution { public: stringminWindow(string s, string t){ map<char,int> mp; for(int i=0;i<t.size();i++) { mp[t[i]]++; } int tot =t.size(); int ret = s.size() + 1; int head = 0, ansHead = 0; for(int i=0;i<s.size();i++) { if(mp.find(s[i])->second>0) tot--; mp.find(s[i])->second--; //尾坐标右移,直接就加入了. while(tot==0) { if (ret > i - head + 1) { ret = i - head + 1; ansHead = head;} // move s[head] if (mp.find(s[ head ])->second == 0) tot ++; mp.find(s[ head ])->second++; head ++; } } if (ret > s.size()) return""; return s.substr(ansHead, ret); } };