[LeetCode] Design HashMap 设计HashMap

Design a HashMap without using any built-in hash table libraries.

To be specific, your design should include these functions:

  • put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
  • get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.

Example:

MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);          
hashMap.put(2, 2);        
hashMap.get(1);            // returns 1
hashMap.get(3);            // returns -1 (not found)
hashMap.put(2, 1);          // update the existing value
hashMap.get(2);            // returns 1
hashMap.remove(2);          // remove the mapping for 2
hashMap.get(2);            // returns -1 (not found)

Note:

    • All keys and values will be in the range of [0, 1000000].
    • The number of operations will be in the range of [1, 10000].
    • Please do not use the built-in HashMap library.

s

原文地址:https://www.cnblogs.com/grandyang/p/9972764.html

时间: 2024-10-10 05:38:36

[LeetCode] Design HashMap 设计HashMap的相关文章

[LeetCode] Design Twitter 设计推特

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweetId): Compo

[LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: A move is guaranteed to be valid and is placed on an empty block.Once a winning condition is reached, no more moves is allowed.A player

[LeetCode] Design HashSet 设计HashSet

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

【Java基础】——HashMap设计原理&实现分析

HashMap在Java开发中有着非常重要的角色地位,每一个Java程序员都应该了解HashMap. 本文主要从源码角度来解析HashMap的设计思路,并且详细地阐述HashMap中的几个概念,并深入探讨HashMap的内部结构和实现细节,讨论HashMap的性能问题,并且在文中贯穿着一些关于HashMap常见问题的讨论. 读完本文,你会了解到:   1. HashMap的设计思路和内部结构组成 2. HashMap中的一些概念: 什么是阀值?为什么会有阀值?什么是加载因子?它们有什么作用? 3

Java基础知识强化之集合框架笔记62:Map集合之HashMap嵌套HashMap

1. HashMap嵌套HashMap  传智播客          jc    基础班                      陈玉楼  20                      高跃     22          jy    就业班                      李杰     21                      曹石磊  23  先存储元素,然后遍历元素 2. 代码示例: 1 package cn.itcast_05; 2 3 import java.uti

Android Design 找设计灵感

借鉴下别人超棒的设计与体验 URL:http://huaban.com/boards/1091038/ URL:https://dribbble.com/ 充分的利用github,可以教你的设计师来用,找不同so easy URL:https://github.com/cameronmcefee/Image-Diff-View-Modes/commit/8e95f70c9c47168305970e91021072673d7cdad8 Android Material Design 统一设计语言规

HashMap和HashMap的区别

首先看两个类 public class HashMap extends AbstractMap implements Map, Cloneable, Serializable public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable      Dictionary 类是任何可将键映射到相应值的类(如 Hashtable)的抽象父类.每个键和每个值都是一个对象.在任何一个 Di

【HashMap 嵌套 HashMap】

package com.yjf.esupplier.common.test; import java.util.HashMap; import java.util.Set; /** * @author shusheng * @description HashMap 嵌套 HashMap * @Email [email protected] * @date 2018/12/18 14:46 */ public class HashMapDemo2 { public static void main

LeetCode 706. Design HashMap (设计哈希映射)

题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. Java Solution: Runtime: 76 ms, faster than 27.53% Memory Usage: 58.2 MB, less than 31.57% 完成日期:03/18/2019 关键点:int array class MyHashMap { int [] map;