problemscpp
A collection of my answers to algorithm problems in c++.
Public 成员函数 | Private 属性 | 静态 Private 属性 | 所有成员列表
leetcode::design_hashmap::MyHashMap类 参考

#include <leetcode.h>

Public 成员函数

 MyHashMap ()
 initializes the object with an empty map. 更多...
 
int get (int key)
 
void put (int key, int value)
 inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value. 更多...
 
void remove (int key)
 removes the key and its corresponding value if the map contains the mapping for the key. 更多...
 

Private 属性

array< list< pair< int, int > >, SZarr
 

静态 Private 属性

static const unsigned SZ = 1021
 

详细描述

在文件 leetcode.h2625 行定义.

构造及析构函数说明

◆ MyHashMap()

leetcode::design_hashmap::MyHashMap::MyHashMap ( )

initializes the object with an empty map.

在文件 leetcode.cpp7116 行定义.

7116 {
7117 arr = array<list<pair<int, int>>, SZ>();
7118 for(unsigned i = 0; i < SZ; i++) {
7119 arr[i] = list<pair<int, int>>();
7120 }
7121 }
static const unsigned SZ
Definition: leetcode.h:2626
array< list< pair< int, int > >, SZ > arr
Definition: leetcode.h:2627

引用了 arr , 以及 SZ.

成员函数说明

◆ get()

int leetcode::design_hashmap::MyHashMap::get ( int  key)
返回
the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.

在文件 leetcode.cpp7134 行定义.

7134 {
7135 const unsigned slot = static_cast<unsigned>(key) % SZ;
7136 for(auto &[k, v]: arr[slot]) {
7137 if(k == key) {
7138 return v;
7139 }
7140 }
7141 return -1;
7142 }

引用了 arr , 以及 SZ.

◆ put()

void leetcode::design_hashmap::MyHashMap::put ( int  key,
int  value 
)

inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.

在文件 leetcode.cpp7123 行定义.

7123 {
7124 const unsigned slot = static_cast<unsigned>(key) % SZ;
7125 for(auto &[k, v]: arr[slot]) {
7126 if(k == key) {
7127 v = value;
7128 return;
7129 }
7130 }
7131 arr[slot].emplace_back(key, value);
7132 }

引用了 arr , 以及 SZ.

◆ remove()

void leetcode::design_hashmap::MyHashMap::remove ( int  key)

removes the key and its corresponding value if the map contains the mapping for the key.

在文件 leetcode.cpp7144 行定义.

7144 {
7145 const unsigned slot = static_cast<unsigned>(key) % SZ;
7146 for(auto it = arr[slot].begin(); it != arr[slot].end(); ++it) {
7147 if(it->first == key) {
7148 arr[slot].erase(it);
7149 return;
7150 }
7151 }
7152 }

引用了 arr , 以及 SZ.

类成员变量说明

◆ arr

array<list<pair<int, int> >, SZ> leetcode::design_hashmap::MyHashMap::arr
private

在文件 leetcode.h2627 行定义.

被这些函数引用 MyHashMap(), get(), put() , 以及 remove().

◆ SZ

const unsigned leetcode::design_hashmap::MyHashMap::SZ = 1021
staticprivate

在文件 leetcode.h2626 行定义.

被这些函数引用 MyHashMap(), get(), put() , 以及 remove().


该类的文档由以下文件生成: