当前位置:网站首页>剑指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;
}
};
边栏推荐
- What is the transport layer protocol tcp/udp???
- 八叉树建立地图并实现路径规划导航
- sklearn clustering聚类
- PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (4)
- 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
- Is CICC Fortune Securities safe? How long does it take to open an account
- Paper:《All Models are Wrong, but Many are Useful: 所有模型都是错误的,但许多模型都是有用的:通过同时研究一整类预测模型来了解变量的重要性》翻译与解读
- Glyphicons V3 字体图标查询
- Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization
- parker泵PV140R1K1T1PMMC
猜你喜欢
![[5 minutes paper] Pointer network](/img/9a/66edc27f08f245447cc6b8867d2383.png)
[5 minutes paper] Pointer network

PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)

QT is the most basic layout, creating a window interface

Understand │ XSS attack, SQL injection, CSRF attack, DDoS attack, DNS hijacking

Kalibr calibration realsensed435i -- multi camera calibration

A comprehensive review of image enhancement technology in deep learning

# 工欲善其事必先利其器-C语言拓展--嵌入式C语言(十一)

TI C6000 TMS320C6678 DSP+ Zynq-7045的ZYNQ PS + PL异构多核案例开发手册(1)

Desktop application layout

bucher齿轮泵QX81-400R301
随机推荐
promise,async-await 和 跨域问题的解决--代理服务器的原理
【EXPDP导出数据】expdp导出23行记录,且不包含lob字段的表,居然用时48分钟,请大家帮忙看看
TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(4)
Digital warehouse: iqiyi digital warehouse platform construction practice
Encryption model
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
Using information entropy to construct decision tree
Pytorch installation CUDA corresponding
反射、枚举以及lambda表达式
请问参数化视图可以根据传入参数的特点得到不同行数的SQL吗?比如这里我想根据传输参数@field中列
gcc/g++与动静库以及gdb
.NET 手动获取注入对象
阿里巴巴一面 :十道经典面试题解析
Understand │ XSS attack, SQL injection, CSRF attack, DDoS attack, DNS hijacking
德国EMG易安基推动器ED301/6 HS
OpenGL learning diary 2 - shaders
Sword finger offer II 009. subarray with product less than k
ES6高级-查询商品案例
泰山OFFICE技术讲座:WORD的缩放比例与显示略有差异
数仓:数仓建设中的数据建模和日志体系