当前位置:网站首页>Sword finger offer special assault edition day 11
Sword finger offer special assault edition day 11
2022-07-26 16:08:00 【hys__ handsome】
The finger of the sword Offer II 033. Morpheme phrase
Sorting method
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;
}
};
Counting writing method , The main optimization here is : Theoretically unordered_map Just go , But the key should be an array of character occurrences , then vector It's getting longer ,unordered_map No, array Corresponding hash function , So you need to define .
The rest of the operation is similar to the sorting method .
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
// Customize to array<int, 26> Hash function of type
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;
}
};
The finger of the sword Offer II 034. Whether alien languages are sorted
Because string comparison defaults to a-z That set , So we only need to map alien characters into a-z that will do
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;
}
};
The finger of the sword Offer II 035. Minimum time difference
After sorting, the difference between a certain number and adjacent numbers is the smallest .
Be careful : Because time is a ring , So finally, we should consider the difference between the beginning and the end .
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); // The time difference between adjacent times
pre = t;
}
res = min(res, t0 + 1440 - pre); // The time difference between the beginning and the end
return res;
}
};
边栏推荐
- hawe螺旋插装式单向阀RK4
- Paper:《All Models are Wrong, but Many are Useful: 所有模型都是错误的,但许多模型都是有用的:通过同时研究一整类预测模型来了解变量的重要性》翻译与解读
- [ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
- 数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
- C # set different text watermarks for each page of word
- Alibaba cloud DMS MySQL cloud database report error, solve!!
- Clojure 运行原理之编译器剖析
- The solution to the display disorder of several events files in the tensorboard
- zabbix 6.2.0部署
- Taishan Office Technology Lecture: the zoom ratio of word is slightly different from the display
猜你喜欢

2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案

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

【万字长文】使用 LSM-Tree 思想基于.Net 6.0 C# 实现 KV 数据库(案例版)

教大模型自己跳过“无用”层,推理速度×3性能不变,谷歌MIT这个新方法火了...

Google Earth engine - merra-2 m2t1nxlv: 1980 present global pressure, temperature, wind and other data sets

API 版本控制【 Eolink 翻译】

PAT甲级 1050 String Subtraction

2022你的安全感是什么?沃尔沃年中问道

Delta controller rmc200

Refuse noise, the entry journey of earphone Xiaobai
随机推荐
Reflection, enumeration, and lambda expressions
Google Earth engine - merra-2 m2t1nxlv: 1980 present global pressure, temperature, wind and other data sets
Paper: all models are wrong, but many are useful: all models are wrong, but many are useful: understand the importance of variables by studying a whole class of prediction models at the same time
SAP ABAP 守护进程的实现方式
A comprehensive review of image enhancement technology in deep learning
一文详解 Redis 中 BigKey、HotKey 的发现与处理
【DSCTF2022】pwn补题记录
初识OpenGL (2)编译着色器
“卡片笔记法”在思源的具体实践案例
gcc/g++与动静库以及gdb
Implementation of personalized healthy diet recommendation system based on SSM
数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
Development daily summary (11): file upload function improvement: Chinese character detection and text content processing
提问征集丨快来向NLLB作者提问啦!(智源Live第24期)
2022年最新北京建筑安全员模拟题库及答案
如何通过ETL调度工具 TASKCTL 使用作业插件类型调用 kettle作业?
小哥自创AI防拖延系统,一玩手机就被“闪瞎” | Reddit高热
基于SSM实现个性化健康饮食推荐系统
Robot hand eye calibration ax=xb (eye to hand and eye in hand) and plane nine point calibration
理解卷积神经网络中的权值共享