zoukankan      html  css  js  c++  java
  • js数据结构hashMap -----hashMap

    // 散列表  hashMap
            // 散列函数
            /**
            给定一个key参数,我们就能根据组成key的每个字符的ASCII码值
            的和得到一个数字。所以, 首先需要一个变量来存储这个总和(行{1})。
            然后,遍历key(行{2})并将从ASCII表中查到 的每个字符对应的ASCII值加到
            hash变量中(可以使用JavaScript的String类中的charCodeAt 方法——行{3})。
            最后,返回hash值。为了得到比较小的数值,我们会使用hash值和一个任意 数
            做除法的余数(mod)
            **/
            var loseloseHashCode = function (key) {
                var hash = 0;                          //{1}
                for (var i = 0; i < key.length; i++) { //{2}
                    hash += key.charCodeAt(i);         //{3}
                }
                return hash % 37; //{4}
            };
    
            var djb2HashCode = function (key) {
                var hash = 5381; //{1}
                for (var i = 0; i < key.length; i++) { //{2}
                    hash = hash * 33 + key.charCodeAt(i); //{3}
                }
                return hash % 1013
            }
            function HashTable() {
                var table = [];
                this.put = function (key, value) {
                    var position = djb2HashCode(key); //{5}
                    console.log(position + ' - ' + key); //{6}
                    table[position] = value; //{7}
                };
                this.get = function (key) {
                    return table[djb2HashCode(key)];
                };
                this.remove = function (key) {
                    table[djb2HashCode(key)] = undefined;
                }
                this.print = function () {
                    for (var i = 0; i < table.length; ++i) { //{1}
                        if (table[i] !== undefined) {        //{2}
                            console.log(i + ": " + table[i]);//{3}
                        }
                    }
                };
            }
            var hash = new HashTable();
            hash.put('Gandalf', 'gandalf@email.com');
            hash.put('John', 'johnsnow@email.com');
            hash.put('Tyrion', 'tyrion@email.com');
    
            console.log(hash.get('Gandalf'));
            console.log(hash.get('Loiane'));
            /**
            有时候,一些键会有相同的散列值。不同的值在散列表中对应相同位置的时候,
            我们称其为冲突。例如,我们看看下面的代码会得到怎样的输出结果
            **/
            var hash = new HashTable();
            hash.put('Gandalf', 'gandalf@email.com');
            hash.put('John', 'johnsnow@email.com');
            hash.put('Tyrion', 'tyrion@email.com');
            hash.put('Aaron', 'aaron@email.com');
            hash.put('Donnie', 'donnie@email.com');
            hash.put('Ana', 'ana@email.com');
            hash.put('Jonathan', 'jonathan@email.com');
            hash.put('Jamie', 'jamie@email.com');
            hash.put('Sue', 'sue@email.com');
            hash.put('Mindy', 'mindy@email.com');
            hash.put('Paul', 'paul@email.com');
            hash.put('Nathan', 'nathan@email.com');
            hash.print()
  • 相关阅读:
    BZOJ 2154 Crash的数字表格 【莫比乌斯反演】
    BZOJ 3529 [Sdoi2014]数表 【莫比乌斯反演】
    BZOJ 2820 YY的GCD 【莫比乌斯反演】
    BZOJ 2440 [中山市选2011]完全平方数 【莫比乌斯反演】
    [BalticOI 2004] Sequence
    AtCoder [ARC070E] NarrowRectangles
    AtCoder [AGC022E] Median Replace
    AtCoder [ARC101E] Ribbons on Tree
    CF107D Crime Management
    Loj 6497「雅礼集训 2018 Day1」图
  • 原文地址:https://www.cnblogs.com/vali/p/9603132.html
Copyright © 2011-2022 走看看