Rehashing解題報(bào)告

Description:

The size of the hash table is not determinate at the very beginning. If the total size of keys is too large (e.g. size >= capacity / 10), we should double the size of the hash table and rehash every keys. Say you have a hash table looks like below:

size=3, capacity=4

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

null
The hash function is:

int hashcode(int key, int capacity) {
return key % capacity;
}
here we have three numbers, 9, 14 and 21, where 21 and 9 share the same position as they all have the same hashcode 1 (21 % 4 = 9 % 4 = 1). We store them in the hash table by linked list.

rehashing this hash table, double the capacity, you will get:

size=3, capacity=8

index: 0 1 2 3 4 5 6 7
hash : [null, 9, null, null, null, 21, 14, null]
Given the original hash table, return the new hash table after rehashing .

Example:

Given [null, 21->9->null, 14->null, null],

return [null, 9->null, null, null, null, 21->null, 14->null, null]

Link:

http://www.lintcode.com/en/problem/rehashing/

Tips:

在重新計(jì)算key的時(shí)候湖饱,如果value是一個(gè)負(fù)數(shù)大咱,那么使用value % capacity這個(gè)公式會產(chǎn)生負(fù)數(shù)喷户,所以需要使用代替公式:
(value % capacity + capacity) % capacity

完整代碼:

vector<ListNode*> rehashing(vector<ListNode*> hashTable) { int capacity = hashTable.size()*2; vector<ListNode*> result(capacity, NULL); for(int i = 0; i < hashTable.size(); i++) { if(hashTable[i] == NULL) continue; else { ListNode* head = hashTable[i]; while(head != NULL) { hashadd(result, head->val); head = head->next; } } } return result; } void hashadd(vector<ListNode*> &result, int val) { int key = hashcode(val, result.size()); if(result[key] == NULL) { ListNode* temp = new ListNode(val); result[key] = temp; } else { ListNode* head = result[key]; while(head->next != NULL) head = head->next; ListNode* temp = new ListNode(val); head->next = temp; } } int hashcode(int key, int capacity) { return (key % capacity + capacity) % capacity; }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嫉你,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子筝家,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拄养,死亡現(xiàn)場離奇詭異,居然都是意外死亡银舱,警方通過查閱死者的電腦和手機(jī)瘪匿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寻馏,“玉大人棋弥,你說我怎么就攤上這事〕锨罚” “怎么了顽染?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長轰绵。 經(jīng)常有香客問我粉寞,道長,這世上最難降的妖魔是什么左腔? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任唧垦,我火速辦了婚禮,結(jié)果婚禮上液样,老公的妹妹穿的比我還像新娘振亮。我一直安慰自己,他們只是感情好蓄愁,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布双炕。 她就那樣靜靜地躺著,像睡著了一般撮抓。 火紅的嫁衣襯著肌膚如雪妇斤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天丹拯,我揣著相機(jī)與錄音站超,去河邊找鬼。 笑死乖酬,一個(gè)胖子當(dāng)著我的面吹牛死相,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咬像,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼算撮,長吁一口氣:“原來是場噩夢啊……” “哼生宛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起肮柜,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤陷舅,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后审洞,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體莱睁,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年芒澜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仰剿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,769評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡痴晦,死狀恐怖南吮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情誊酌,我是刑警寧澤旨袒,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站术辐,受9級特大地震影響砚尽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辉词,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一必孤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瑞躺,春花似錦敷搪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至捞镰,卻和暖如春闸与,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背岸售。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工践樱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人凸丸。 一個(gè)月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓拷邢,卻偏偏與公主長得像,于是被迫代替她去往敵國和親屎慢。 傳聞我的和親對象是個(gè)殘疾皇子瞭稼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容