コード例 #1
0
ファイル: MyHashMap.cs プロジェクト: deyishi/LeetCode
        private HashMapListNode FindBucketNode(HashMapListNode head, int key)
        {
            HashMapListNode curr = head;

            while (curr != null && curr.Next != null && curr.Next.Key != key)
            {
                curr = curr.Next;
            }

            return(curr);
        }
コード例 #2
0
ファイル: MyHashMap.cs プロジェクト: deyishi/LeetCode
        /** 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)
        {
            var bucketKey = Hash(key);

            if (_buckets[bucketKey] == null)
            {
                return(-1);
            }

            HashMapListNode head = FindBucketNode(_buckets[bucketKey], key);

            return(head.Next?.Value ?? -1);
        }
コード例 #3
0
ファイル: MyHashMap.cs プロジェクト: deyishi/LeetCode
        /** Removes the mapping of the specified value key if this map contains a mapping for the key */
        public void Remove(int key)
        {
            var bucketKey = Hash(key);

            if (_buckets[bucketKey] == null)
            {
                return;
            }

            HashMapListNode head = FindBucketNode(_buckets[bucketKey], key);

            if (head.Next != null)
            {
                head.Next = head.Next.Next;
            }
        }
コード例 #4
0
ファイル: MyHashMap.cs プロジェクト: deyishi/LeetCode
        /** value will always be non-negative. */
        public void Put(int key, int value)
        {
            var bucketKey = Hash(key);

            if (_buckets[bucketKey] == null)
            {
                _buckets[bucketKey] = new HashMapListNode(-1, -1);
            }

            HashMapListNode head = FindBucketNode(_buckets[bucketKey], key);

            if (head.Next == null)
            {
                head.Next = new HashMapListNode(key, value);
            }
            else
            {
                head.Next.Value = value;
            }
        }