实现一个资料结构,能支持下面4个操作:
执行每个操作,时间复杂度都要求为 O(1)
class AllOne {
public:
/** Initialize your data structure here. */
map<string,int> Data;
AllOne() {
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
void inc(string key) {
Data[key]++;
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
map<string,int>::iterator index = Data.find(key);
if(index!=Data.end()){
if(index->second==1){
Data.erase(key);
}
else{
index->second--;
}
}
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
if(Data.size() == 0) return "";
map<string, int>::iterator Max = Data.begin();
for(map<string,int>::iterator index = Data.begin(); index != Data.end(); index++){
if(Max->second < index->second) {
Max = index;
}
}
return Max->first;
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
if(Data.size() == 0) return "";
map<string, int>::iterator Min = Data.begin();
for(map<string,int>::iterator index = Data.begin(); index != Data.end(); index++){
if(Min->second > index->second) {
Min = index;
}
}
return Min->first;
}
};
<<: [Day29] VSCode Plugin - Other
>>: Day29--Bootstrap&CSS文字排版&样式(7)
Q1. Git 是什麽? Git 是一个分散式版本控管软件,每个开发者手中都会有完整的一份副本,包含...
铁人赛终於来到尾声... 再也不用担心忘记发文了!! Sport vector created by...
甚麽是XSS 针对网站有很多种攻击方式, SQL Injection是一种, 另外还有一种常见的是X...
Bugcrowd Vulnerability Rating Taxonomy (VRT) 最後一篇我...
简介 上一篇介绍了如何利用2进位来表示10进位的数字,这次则要再进阶的介绍一下4、8、16进位。在下...