赞
踩
优点
缺点
其实就是里面有一个数组(指针数组),里面放的是双向链表,双向链表里面放的是一个pair,pair里放的是key,value,单向的也可以(单向的删除要找的是删除元素的上一个,双向的找对应的就好)
class MyHashMap {
public:
static int hsah(int key) {
return key%size;
}
/** Initialize your data structure here. */
MyHashMap():mymap_(size) {
}
/** value will always be non-negative. */
void put(int key, int value) {
int index = hsah(key);
// cout<<"put"<<endl;
// cout<<"index: "<<index<<endl;
auto iter = mymap_[index].begin();
while(iter!=mymap_[index].end()) {
if(iter->first==key) {
iter->second=value;
return;
}
iter++;
}
mymap_[index].insert(mymap_[index].end(),{key,value});
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
// cout<<"get: "<<key<<endl;
int index = hsah(key);
// for(auto i = mymap_[index].begin();i!=mymap_[index].end();i++) {
// cout<<i->first<<" "<<i->second<<" | ";
// }
// cout<<endl;
auto iter = mymap_[index].begin();
for(;iter!=mymap_[index].end();iter++) {
if(iter->first==key) {
return iter->second;
}
}
return -1;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
// cout<<"remove"<<endl;
int index = hsah(key);
for( auto iter = mymap_[index].begin();iter!=mymap_[index].end();iter++) {
if(iter->first==key) {
iter=mymap_[index].erase(iter); // 后面的迭代器虽然不会失效但是不能用++了
// iter--;
return;
}
}
}
private:
vector<list<pair<int,int>>> mymap_;
static const int size = 572;
};
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap* obj = new MyHashMap();
* obj->put(key,value);
* int param_2 = obj->get(key);
* obj->remove(key);
*/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。