LintCode 129 [Rehashing]

原题

哈希表容量的大小在一开始是不确定的。如果哈希表存储的元素太多(如超过容量的十分之一),我们应该将哈希表容量扩大一倍,并将所有的哈希值重新安排。假设你有如下一哈希表:
size=3, capacity=4

[null, 21, 14, null]
       ↓    ↓
       9   null
       ↓
      null

哈希函数为:

int hashcode(int key, int capacity) {
    return key % capacity;
}

这里有三个数字9,14,21,其中21和9共享同一个位置因为它们有相同的哈希值1(21 % 4 = 9 % 4 = 1)。我们将它们存储在同一个链表中。
重建哈希表,将容量扩大一倍,我们将会得到:
size=3, capacity=8

index:   0    1    2    3     4    5    6   7
hash : [null, 9, null, null, null, 21, 14, null]

给定一个哈希表,返回重哈希后的哈希表。

解题思路

  • 当哈希表的size不够大的时候,动态增加哈希表的大小
  • 第一步容量扩大一倍,loop之前hash表的所有listnode,逐个拷贝

完整代码

"""
Definition of ListNode
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""
class Solution:
    """
    @param hashTable: A list of The first node of linked list
    @return: A list of The first node of linked list which have twice size
    """
    def rehashing(self, hashTable):
        if len(hashTable) <= 0:
            return hashTable
            
        newcapacity = 2 * len(hashTable)
        newTable = [None for i in range(newcapacity)]
        for listNode in hashTable:
            while listNode != None:
                newindex = (listNode.val % newcapacity + newcapacity) % newcapacity
                if newTable[newindex] == None:
                    newTable[newindex] = ListNode(listNode.val)
                else:
                    dummy = newTable[newindex]
                    while dummy.next != None:
                        dummy = dummy.next
                    dummy.next = ListNode(listNode.val)
                listNode = listNode.next
        return newTable

你可能感兴趣的:(LintCode 129 [Rehashing])