日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java关于hashmap编程题_LeetCode算法题-Design HashMap(Java实现)

發布時間:2024/7/19 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java关于hashmap编程题_LeetCode算法题-Design HashMap(Java实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是悅樂書的第299次更新,第318篇原創

01 看題和準備

今天介紹的是LeetCode算法題中Easy級別的第167題(順位題號是706)。在不使用任何內置哈希表庫的情況下設計HashMap。具體而言,你的設計應包括以下功能:

put(key,value):將一個(key,value)對插入HashMap。如果該值已存在于HashMap中,請更新該值。

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(未找到)

注意:

所有key和value都將在[0,1000000]范圍內。

操作次數將在[1,10000]范圍內。

請不要使用內置的HashMap庫。

本次解題使用的開發工具是eclipse,jdk使用的版本是1.8,環境是win7 64位系統,使用Java語言編寫和測試。

02 第一種解法

為了實現鍵值對的效果,內層使用了二維數組,外層使用ArrayList。其中二維數組是一個一行兩列的結構,第一行第一列存儲key的值,第一行第二列存儲value的值。另外,我們還需要單獨寫一個contains方法,來判斷當前的key是否存在于HashMap中。

put方法的時間復雜度為O(n),get方法的時間復雜度為O(n),remove的時間復雜度為O(n),contains方法的時間復雜度為O(n)。

class MyHashMap {

List list;

/** Initialize your data structure here. */

public MyHashMap() {

list = new ArrayList();

}

/** value will always be non-negative. */

public void put(int key, int value) {

if (contains(key)) {

for (int[][] arr : list) {

if (arr != null && arr[0][0] == key) {

arr[0][1] = value;

break;

}

}

} else {

list.add(new int[][]{{key, value}});

}

}

public boolean contains(int key){

for (int[][] arr : list) {

if (arr != null && arr[0][0] == key) {

return true;

}

}

return false;

}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */

public int get(int key) {

for (int[][] arr : list) {

if (arr != null && arr[0][0] == key) {

return arr[0][1];

}

}

return -1;

}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */

public void remove(int key) {

if (contains(key)) {

for (int i=0; i

if (list.get(i)[0][0] == key) {

list.remove(i);

break;

}

}

}

}

}

/**

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

*/

03 第二種解法

我們也可以使用一個小track,依舊使用數組,但是變成了一維數組,因為題目給定了key和value的范圍,所以數組的容量為其最大范圍加1。因為最小值可以為0,而數組默認值是0,避免誤判,將數組的初始值全部賦值為-1。

put方法的時間復雜度為O(1),get方法的時間復雜度為O(1),remove的時間復雜度為O(1),但是空間復雜度較高。

class MyHashMap {

int[] arr;

/** Initialize your data structure here. */

public MyHashMap() {

arr = new int[1000001];

Arrays.fill(arr, -1);

}

/** value will always be non-negative. */

public void put(int key, int value) {

arr[key] = value;

}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */

public int get(int key) {

return arr[key];

}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */

public void remove(int key) {

arr[key] = -1;

}

}

/**

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

*/

04 第三種解法

class MyHashMap {

/** Initialize your data structure here. */

ListNode[] nodes;

public MyHashMap() {

nodes = new ListNode[10000];

}

/** value will always be non-negative. */

public void put(int key, int value) {

int idx = key%10000;

if(nodes[idx]==null){

nodes[idx] = new ListNode(-1,-1);

nodes[idx].next = new ListNode(key,value);

}else{

ListNode pre = find(nodes[idx], key);

if(pre.next == null){

pre.next = new ListNode(key, value);

}else{

pre.next.value = value;

}

}

}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */

public int get(int key) {

int idx = key%10000;

if(nodes[idx] == null) return -1;

else{

ListNode pre = find(nodes[idx], key);

if(pre.next == null) return -1;

else return pre.next.value;

}

}

public ListNode find(ListNode root, int key){

ListNode pre = null;

ListNode cur = root;

while(cur!=null && cur.key != key){

pre = cur;

cur = cur.next;

}

return pre;

}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */

public void remove(int key) {

int idx = key%10000;

if(nodes[idx] == null) return;

else{

ListNode pre = find(nodes[idx], key);

if(pre.next == null) return;

else{

pre.next = pre.next.next;

}

}

}

class ListNode{

int key;

int value;

ListNode next;

ListNode(int key, int value){

this.key = key;

this.value = value;

}

}

}

/**

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

*/

05 小結

算法專題目前已日更超過四個月,算法題文章167+篇,公眾號對話框回復【數據結構與算法】、【算法】、【數據結構】中的任一關鍵詞,獲取系列文章合集。

以上就是全部內容,如果大家有什么好的解法思路、建議或者其他問題,可以下方留言交流,點贊、留言、轉發就是對我最大的回報和支持!

總結

以上是生活随笔為你收集整理的java关于hashmap编程题_LeetCode算法题-Design HashMap(Java实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。