leetcode 706. 設計哈希映射

不使用任何內建的哈希表庫設計一個哈希映射(HashMap)。

實現 MyHashMap 類:

MyHashMap() 用空映射初始化對象
void put(int key, int value) 向 HashMap 插入一個鍵值對 (key, value) 。如果 key 已經存在于映射中,則更新其對應的值 value 。
int get(int key) 返回特定的 key 所映射的 value ;如果映射中不包含 key 的映射,返回 -1 。
void remove(key) 如果映射中存在 key 的映射,則移除 key 和它所對應的 value 。

示例:

輸入:
[“MyHashMap”, “put”, “put”, “get”, “get”, “put”, “get”, “remove”, “get”]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
輸出:
[null, null, null, 1, -1, null, 1, null, -1]

解釋:
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // myHashMap 現在為 [[1,1]]
myHashMap.put(2, 2); // myHashMap 現在為 [[1,1], [2,2]]
myHashMap.get(1); // 返回 1 ,myHashMap 現在為 [[1,1], [2,2]]
myHashMap.get(3); // 返回 -1(未找到),myHashMap 現在為 [[1,1], [2,2]]
myHashMap.put(2, 1); // myHashMap 現在為 [[1,1], [2,1]](更新已有的值)
myHashMap.get(2); // 返回 1 ,myHashMap 現在為 [[1,1], [2,1]]
myHashMap.remove(2); // 刪除鍵為 2 的數據,myHashMap 現在為 [[1,1]]
myHashMap.get(2); // 返回 -1(未找到),myHashMap 現在為 [[1,1]]

提示:

0 <= key, value <= 106
最多調用 104 次 put、get 和 remove 方法

解題思路

利用 Integer數組實現,下標作為key,數組內元素為value

代碼

    class MyHashMap {Integer[] hash;/** Initialize your data structure here. */public MyHashMap() {hash=new Integer[1000001];}/** value will always be non-negative. */public void put(int key, int value) {hash[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 hash[key]==null?-1:hash[key];}/** Removes the mapping of the specified value key if this map contains a mapping for the key */public void remove(int key) {hash[key]=null;}}/*** 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);*/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391616.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391616.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391616.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據庫語言 數據查詢_使用這種簡單的查詢語言開始查詢數據

數據庫語言 數據查詢Working with data is becoming an increasingly important skill in the modern workplace. 在現代工作場所中&#xff0c;處理數據已成為越來越重要的技能。 Data is no longer the domain of analysts and software engineers. With todays technology,…

面向對象編程思想-觀察者模式

一、引言 相信猿友都大大小小經歷過一些面試&#xff0c;其中有道經典題目&#xff0c;場景是貓咪叫了一聲&#xff0c;老鼠跑了&#xff0c;主人被驚醒&#xff08;設計有擴展性的可加分&#xff09;。對于初學者來說&#xff0c;可能一臉懵逼&#xff0c;這啥跟啥啊是&#x…

typescript 使用_如何使用TypeScript輕松修改Minecraft

typescript 使用by Josh Wulf通過喬什沃爾夫(Josh Wulf) 如何使用TypeScript輕松修改Minecraft (How to modify Minecraft the easy way with TypeScript) Usually, modifying Minecraft requires coding in Java, and a lot of scaffolding. Now you can write and share Min…

Python:在Pandas數據框中查找缺失值

How to find Missing values in a data frame using Python/Pandas如何使用Python / Pandas查找數據框中的缺失值 介紹&#xff1a; (Introduction:) When you start working on any data science project the data you are provided is never clean. One of the most common …

監督學習-回歸分析

一、數學建模概述 監督學習&#xff1a;通過已有的訓練樣本進行訓練得到一個最優模型&#xff0c;再利用這個模型將所有的輸入映射為相應的輸出。監督學習根據輸出數據又分為回歸問題&#xff08;regression&#xff09;和分類問題&#xff08;classfication&#xff09;&#…

leetcode 54. 螺旋矩陣(遞歸)

給你一個 m 行 n 列的矩陣 matrix &#xff0c;請按照 順時針螺旋順序 &#xff0c;返回矩陣中的所有元素。 示例 1&#xff1a; 輸入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 輸出&#xff1a;[1,2,3,6,9,8,7,4,5] 示例 2&#xff1a; 輸入&#xff1a;matrix [[1,…

微服務架構技能

2019獨角獸企業重金招聘Python工程師標準>>> 微服務架構技能 博客分類&#xff1a; 架構 &#xff08;StuQ 微服務技能圖譜&#xff09; 2課程簡介 本課程分為基礎篇和高級篇兩部分&#xff0c;旨在通過完整的案例&#xff0c;呈現微服務的開發、測試、構建、部署、…

phpstorm 調試_PhpStorm中的多用戶調試

phpstorm 調試by Ray Naldo雷納爾多(Ray Naldo) PhpStorm中的多用戶調試 (Multi-User Debugging in PhpStorm) 使用Xdebug和DBGp代理 (Using Xdebug and DBGp Proxy) “Er, wait a minute… Don’t you just use xdebug.remote_connect_back which has been introduced since …

Tableau Desktop認證:為什么要關心以及如何通過

Woah, Tableau!哇&#xff0c;Tableau&#xff01; By now, almost everyone’s heard of the data visualization software that brought visual analytics to the public. Its intuitive drag and drop interface makes connecting to data, creating graphs, and sharing d…

約束布局constraint-layout導入失敗的解決方案 - 轉

今天有同事用到了約束布局&#xff0c;但是導入我的工程出現錯誤 **提示錯誤&#xff1a; Could not find com.Android.support.constraint:constraint-layout:1.0.0-alpha3** 我網上查了一下資料&#xff0c;都說是因為我的androidStudio版本是最新的穩定版導入這個包就會報這…

算法復習:冒泡排序

思想&#xff1a;對于一個列表,每個數都是一個"氣泡 "&#xff0c;數字越大表示"越重 "&#xff0c;最重的氣泡移動到列表最后一位&#xff0c;冒泡排序后的結果就是“氣泡”按照它們的重量依次移動到列表中它們相應的位置。 算法&#xff1a;搜索整個列表…

leetcode 59. 螺旋矩陣 II(遞歸)

給你一個正整數 n &#xff0c;生成一個包含 1 到 n2 所有元素&#xff0c;且元素按順時針順序螺旋排列的 n x n 正方形矩陣 matrix 。 示例 1&#xff1a; 輸入&#xff1a;n 3 輸出&#xff1a;[[1,2,3],[8,9,4],[7,6,5]] 解題思路 按層進行數字的填充&#xff0c;每一層…

前端基礎進階(七):函數與函數式編程

縱觀JavaScript中所有必須需要掌握的重點知識中&#xff0c;函數是我們在初學的時候最容易忽視的一個知識點。在學習的過程中&#xff0c;可能會有很多人、很多文章告訴你面向對象很重要&#xff0c;原型很重要&#xff0c;可是卻很少有人告訴你&#xff0c;面向對象中所有的重…

期權數據 獲取_我如何免費獲得期權數據

期權數據 獲取by Harry Sauers哈里紹爾斯(Harry Sauers) 我如何免費獲得期權數據 (How I get options data for free) 網頁抓取金融簡介 (An introduction to web scraping for finance) Ever wished you could access historical options data, but got blocked by a paywall…

顯示與刪除使用工具

右擊工具菜單欄中的空白處選擇自定義 在彈出的自定義菜單中選擇命令選項在選擇想要往里面添加工具的菜單&#xff0c;之后在選擇要添加的工具 若想要刪除工具欄中的某個工具&#xff0c;在打開自定義菜單后&#xff0c;按住鼠標左鍵拖動要刪除工具到空白處 例如 轉載于:https:/…

js值的拷貝和值的引用_到達P值的底部:直觀的解釋

js值的拷貝和值的引用介紹 (Introduction) Welcome to this lesson on calculating p-values.歡迎參加有關計算p值的課程。 Before we jump into how to calculate a p-value, it’s important to think about what the p-value is really for.在我們開始計算p值之前&#xff…

leetcode 115. 不同的子序列(dp)

給定一個字符串 s 和一個字符串 t &#xff0c;計算在 s 的子序列中 t 出現的個數。 字符串的一個 子序列 是指&#xff0c;通過刪除一些&#xff08;也可以不刪除&#xff09;字符且不干擾剩余字符相對位置所組成的新字符串。&#xff08;例如&#xff0c;“ACE” 是 “ABCDE…

監督學習-KNN最鄰近分類算法

分類&#xff08;Classification&#xff09;指的是從數據中選出已經分好類的訓練集&#xff0c;在該訓練集上運用數據挖掘分類的技術建立分類模型&#xff0c;從而對沒有分類的數據進行分類的分析方法。 分類問題的應用場景&#xff1a;用于將事物打上一個標簽&#xff0c;通常…

istio 和 kong_如何啟動和運行Istio

istio 和 kongby Chris Cooney克里斯庫尼(Chris Cooney) 如何啟動和運行Istio (How to get Istio up and running) 而一旦完成&#xff0c;您就可以做的瘋狂的事情。 (And the crazy stuff you can do once it is.) The moment you get Istio working on your cluster, it fee…

js練習--貪吃蛇(轉)

最近一直在看javascript&#xff0c;但是發現不了動力。就開始想找動力&#xff0c;于是在網上找到了一個用js寫的貪吃蛇游戲。奈何還不會用git&#xff0c;就只能先這樣保存著。哈哈哈&#xff0c;這也算第一篇博客了&#xff0c;以后會堅持用自己的代碼寫博客的&#xff0c;下…