当前位置:网站首页>剑指offer专项突击版第11天
剑指offer专项突击版第11天
2022-07-26 15:47:00 【hys__handsome】
排序做法
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> mp;
for (string& str: strs) {
string key = str;
sort(key.begin(), key.end());
mp[key].emplace_back(str);
}
vector<vector<string>> ans;
for (auto it = mp.begin(); it != mp.end(); ++it) {
ans.emplace_back(it->second);
}
return ans;
}
};
计数写法,这里主要优化是:理论上unordered_map就行,但是键应该是字符出现次数的数组,然后vector是变长的,unordered_map没有array对应的哈希函数,所以需要自己定义。
剩下的操作就跟排序做法大同小异了。
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
// 自定义对 array<int, 26> 类型的哈希函数
auto arrayHash = [fn = hash<int>{
}] (const array<int, 26>& arr) -> size_t {
return accumulate(arr.begin(), arr.end(), 0u, [&](size_t acc, int num) {
return (acc << 1) ^ fn(num);
});
};
unordered_map<array<int, 26>, vector<string>, decltype(arrayHash)> mp(0, arrayHash);
for (string& str: strs) {
array<int, 26> counts{
};
int length = str.length();
for (int i = 0; i < length; ++i) {
counts[str[i] - 'a'] ++;
}
mp[counts].emplace_back(str);
}
vector<vector<string>> ans;
for (auto it = mp.begin(); it != mp.end(); ++it) {
ans.emplace_back(it->second);
}
return ans;
}
};
因为字符串比较默认的是a-z那一套,所以我们只需要把外星字符根据优先级映射成a-z即可
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
unordered_map<char,char> um;
for(int i = 0; i < order.size(); i++) {
um[order[i]] = 'a' + i;
}
string tmp;
for(auto word: words) {
for(int i = 0; i < word.size(); i++){
word[i] = um[word[i]];
}
if(word < tmp) return false;
tmp = word;
}
return true;
}
};
排序后某个数与相邻数之间相差最小。
注意:因为时间是个环,所以最后要考虑首尾差值。
class Solution {
int get_min(string &t) {
return (int(t[0] - '0') * 10 + int(t[1] - '0')) * 60 + int(t[3] - '0') * 10 + int(t[4] - '0');
}
public:
int findMinDifference(vector<string> &timePoints) {
int n = timePoints.size();
if (n > 1440) {
return 0;
}
sort(timePoints.begin(), timePoints.end());
int res = 0x3f3f3f3f;
int t0 = get_min(timePoints[0]);
int pre = t0;
for (int i = 1; i < n; ++i) {
int t = get_min(timePoints[i]);
res = min(res, t - pre); // 相邻时间的时间差
pre = t;
}
res = min(res, t0 + 1440 - pre); // 首尾时间的时间差
return res;
}
};
边栏推荐
- 邻接矩阵的COO格式
- Using information entropy to construct decision tree
- 中金财富炒股安全吗 手续费最便宜的证券公司
- 教程篇(7.0) 05. 通过FortiClient EMS发放FortiClient * FortiClient EMS * Fortinet 网络安全专家 NSE 5
- Deep packet inspection using cuckoo filter paper summary
- My brother created his own AI anti procrastination system, and he was "blinded" when playing with his mobile phone | reddit was hot
- SAP ABAP 守护进程的实现方式
- 2022 what is your sense of security? Volvo asked in the middle of the year
- parker电磁阀D1VW020DNYPZ5
- 请问参数化视图可以根据传入参数的特点得到不同行数的SQL吗?比如这里我想根据传输参数@field中列
猜你喜欢

换把人体工学椅,缓解久坐写代码的老腰吧~

Musk was exposed to be the founder of Google: he broke up his best friend's second marriage and knelt down to beg for forgiveness

数仓:爱奇艺数仓平台建设实践

一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持

sklearn clustering聚类

Implementation of personalized healthy diet recommendation system based on SSM

OSPF综合实验

Pytorch installation CUDA corresponding
![[dsctf2022] PWN supplementary question record](/img/fa/ea26fc0861224df4c391942075eaaf.png)
[dsctf2022] PWN supplementary question record

Desktop application layout
随机推荐
教程篇(7.0) 05. 通过FortiClient EMS发放FortiClient * FortiClient EMS * Fortinet 网络安全专家 NSE 5
提问征集丨快来向NLLB作者提问啦!(智源Live第24期)
OSPF综合实验
开发日常小结(11):文件上传功能改进:中文字符检测与文本内容处理
We were tossed all night by a Kong performance bug
中金财富证券安全吗 开户要多久
API version control [eolink translation]
Promise, async await and the solution of cross domain problems -- the principle of proxy server
Strengthen the defense line of ecological security, and carry out emergency drills for environmental emergencies in Guangzhou
Super simple! It only takes a few steps to customize the weather assistant for TA!!
bucher齿轮泵QX81-400R301
VS2019Debug模式太卡进不去断点
[five minute paper] reinforcement learning based on parameterized action space
Development and implementation of campus epidemic prevention and control management system based on SSM
第七章 在 REST 服务中支持 CORS
数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
换把人体工学椅,缓解久坐写代码的老腰吧~
How to convert planning map into vector data with longitude and latitude geojson
Basic specification of component development, localstorage and sessionstorage, object data to basic value, prototype chain use
Pytorch installation CUDA corresponding