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;
    /** Initialize your data structure here. */
    public MyHashMap() {
        map = new int[1000001];
        Arrays.fill(map, -1);
    }

    /** value will always be non-negative. */
    public void put(int key, int value) {
        map[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 map[key];
    }

    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    public void remove(int key) {
        map[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);
 */

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/10888012.html

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

LeetCode 706. Design HashMap (设计哈希映射)的相关文章

LeetCode 705. Design HashSet (设计哈希集合)

题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java Solution: Runtime: 58 ms, faster than 90.21% Memory Usage: 56.3 MB, less than 68.53% 完成日期:03/18/2019 关键点:boolean array class MyHashSet { boolean [] se

LeetCode 706. Design HashMap

原题链接在这里:https://leetcode.com/problems/design-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

706. Design HashMap - LeetCode

Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为1000000,那构造一个1000000的数组 Java实现: class MyHashMap { int[] table; /** Initialize your data structure here. */ public MyHashMap() { table = new int[1000000]; Ar

706.设计哈希映射

2020-04-08 设计哈希映射 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1. remove(key):如果映射中存在这个键,删除这个数值对. 题解: 思路1: 使用对象设计哈希集合 /** * Initialize your data structure here. */ va

leetcode 705 设计哈希映射

一  题目概述 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. remove(value):将给定值从哈希集合中删除.如果哈希集合中没有这个值,什么也不做. 二 java算法实现 class MyHashSet { private Node[]arr=new Node[1024]; //2的指数,计算hash时可以用 & private class N

[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.

[LeetCode] 355. 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] 534. Design TinyURL 设计短网址

Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design a URL shortening service that is similar to TinyURL? Background:TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com

706. Design 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.