33)变位词组
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> hash;
for(auto str : strs){
auto temp = str;
sort(temp.begin(), temp.end());
hash[temp].push_back(str);
}
for(auto vec : hash){
res.push_back(vec.second);
}
return res;
}
};
34)外星语言是否排序
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
vector<int> vec(26);
for(int i=0; i<order.size(); i++){
vec[order[i]-'a'] = i;
}
for(int i=0; i<words.size()-1; i++){
string s1 = words[i];
string s2 = words[i+1];
for(char j=0; j<max(s1.size(), s2.size()); j++){
int l1 = j>=s1.size() ? -1 : vec[s1[j]-'a'];
int l2 = j>=s2.size() ? -1 : vec[s2[j]-'a'];
if(l1 > l2) return false;
if(l1 < l2) break;
}
}
return true;
}
};
35)最小时间差
class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
if(timePoints.size() >= 24*60) return 0;
vector<int> mins;
for(auto time : timePoints){
mins.push_back(stoi(time.substr(0, 2))*60 + stoi(time.substr(3, 2)));
}
sort(mins.begin(), mins.end());
mins.push_back(mins[0]+24*60);
int res = 24*60;
for(int i=0; i<mins.size()-1; i++){
res = min(res, mins[i+1]-mins[i]);
}
return res;
}
};