706 設計雜湊對映

2021-08-30 14:06:18 字數 1239 閱讀 9941

/*

不使用任何內建的雜湊表庫設計乙個雜湊對映

具體地說,你的設計應該包含以下的功能

put(key, value):向雜湊對映中插入(鍵,值)的數值對。如果鍵對應的值已經存在,更新這個值。

get(key):返回給定的鍵所對應的值,如果對映中不包含這個鍵,返回-1。

remove(key):如果對映中存在這個鍵,刪除這個數值對。

myhashmap hashmap = new myhashmap();

hashmap.put(1, 1);

hashmap.put(2, 2);

hashmap.get(1); // 返回 1

hashmap.get(3); // 返回 -1 (未找到)

hashmap.put(2, 1); // 更新已有的值

hashmap.get(2); // 返回 1

hashmap.remove(2); // 刪除鍵為2的資料

hashmap.get(2); // 返回 -1 (未找到)

*/public

class

myhashmap

}/** value will always be positive. */

public

void

put(

int key,

int value)

public

intget

(int key)

public

void

remove

(int key)

}/**

* 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);

*/

測試及結果

@test

public

void

testleetcode()

/* 1

-11 -1

*/

706 設計雜湊對映

不使用任何內建的雜湊表庫設計乙個雜湊對映 具體地說,你的設計應該包含以下的功能 put key,value 向雜湊對映中插入 鍵,值 的數值對。如果鍵對應的值已經存在,更新這個值。get key 返回給定的鍵所對應的值,如果對映中不包含這個鍵,返回 1。remove key 如果對映中存在這個鍵,刪...

706 設計雜湊對映

題目描述 不使用任何內建的雜湊表庫設計乙個雜湊對映 具體地說,你的設計應該包含以下的功能 put key,value 向雜湊對映中插入 鍵,值 的數值對。如果鍵對應的值已經存在,更新這個值。get key 返回給定的鍵所對應的值,如果對映中不包含這個鍵,返回 1。remove key 如果對映中存在...

706設計雜湊對映

不使用任何內建的雜湊表庫設計乙個雜湊對映 具體地說,你的設計應該包含以下的功能 put key,value 向雜湊對映中插入 鍵,值 的數值對。如果鍵對應的值已經存在,更新這個值。get key 返回給定的鍵所對應的值,如果對映中不包含這個鍵,返回 1。remove key 如果對映中存在這個鍵,刪...