LintCode - Rehashing

本文介绍了一种哈希表扩容算法,通过再散列将原有哈希表中的元素重新分布到新的更大的哈希表中,以解决冲突和提高性能。详细展示了使用C++实现的再散列过程,包括如何计算新的散列位置和链表插入操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Solution {
public: 
    vector<ListNode*> rehashing(vector<ListNode*> hashTable) {
        int n = hashTable.size(), m=n*2;
        vector<ListNode*> res(m, NULL);
        for(int i=0;i<n;i++){
            ListNode* cur = hashTable[i];
            while(cur){
                int val = cur->val;
                ListNode* node = new ListNode(val);
                int index = val<0 ? (val%m+m)%m : val%m;
                ListNode* ccur = res[index];
                if(!ccur) res[index]=node;
                else{
                    while(ccur->next){
                        ccur = ccur->next;
                    }
                    ccur->next = node;
                }
                cur = cur->next;
            }
        }
        return res;
    }
};

 

D:\Mysql\bin>mysql -uroot -fIIrem7oIb.& mysql Ver 9.2.0 for Win64 on x86_64 (MySQL Community Server - GPL) Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Usage: mysql [OPTIONS] [database] -?, --help Display this help and exit. -I, --help Synonym for -? --auto-rehash Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash. (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect. --auto-vertical-output Automatically switch to vertical output mode if the result is wider than the terminal width. -B, --batch Don't use history file. Disable interactive behavior. (Enables --silent.) --bind-address=name IP address to bind to. --binary-as-hex Print binary data as hex. Enabled by default for interactive terminals. --character-sets-dir=name Directory for character set files. --column-type-info Display column type information. -c, --comments Preserve comments. Send comments to the server. The default is --comments (keep comments), disable with --skip-comments. (Defaults to on; use --skip-comments to disable.) -C, --compress Use compression in server/client protocol. -#, --debug[=#] This is a non-debug version. Catch this and exit. --deb
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值