這篇是 Udemy 上的知名課程 – Master the Coding Interview 的部分進修心得。這篇對應的內容是「Data Structures: Hash Tables」關於陣列的第一部分簡介。
課程相關資訊
[連結]:https://www.udemy.com/course/master-the-coding-interview-data-structures-algorithms/
課程對應章節:74~79
請注意:本系列文章為個人對應課程的消化吸收後,所整理出來的內容。換言之,並不一定會包含全部的課程內容,也有可能會添加其他資源來說明。
內容
背景知識
1. 每個程式語言多半已內建 Hash Table 這類的資料結構(一連串的 key-value pair),像是 JavaScript – Object、Python – Dictionary、JAVA – Map、Ruby – Hash
2. 輸入 key,經由快速的 hash function 運算後,得到一串看似亂碼的 key 當成記憶體位置,並儲存對應的值。這是一個單向的過程。
3. Hash Table 在 Insert, Lookup, Delete 和 Search 的時間複雜度都是 O(1),不過你得先知道對應的 key 才行。
4. 當你的 Table 不夠大到容納的下 key 的數量,那就有機會發生 Hash Collision。也就是指同一個 key 會被兩筆以上的資料所呼叫到,也就發生了衝突。
範例程式碼
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class HashTable{ constructor(size){ this.data = new Array(size); } /*產生 hash 的函式*/ _hash(key){ let hash = 0 for(let i=0; i<key.length;i++){ hash = (hash + key.charCodeAt(i) * i ) % this.data.length } return hash } set(key, value){ let address = this._hash(key) if(!this.data[address]){ this.data[address] = [] } this.data[address].push([key, value]) } get(key){ let address = this._hash(key) const currentBucket = this.data[address]; if(currentBucket){ for(let i=0; i< currentBucket.length; i++){ if(currentBucket[i][0] === key){ return currentBucket[i][1] } } } return undefined } } const myHashTable = new HashTable(10) myHashTable.set("grape",10000) myHashTable.set("apples",54) myHashTable.get("apples") // 54 |
相關文章
★全文分享★ [筆記] Master The Coding Interview – 16
![[筆記] Master The Coding Interview – 16 [筆記] Master The Coding Interview – 16](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 15
![[筆記] Master The Coding Interview – 15 [筆記] Master The Coding Interview – 15](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 14
![[筆記] Master The Coding Interview – 14 [筆記] Master The Coding Interview – 14](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 13
![[筆記] Master The Coding Interview – 13 [筆記] Master The Coding Interview – 13](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 12
![[筆記] Master The Coding Interview – 12 [筆記] Master The Coding Interview – 12](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 11
![[筆記] Master The Coding Interview – 11 [筆記] Master The Coding Interview – 11](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 10
![[筆記] Master The Coding Interview – 10 [筆記] Master The Coding Interview – 10](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 9
![[筆記] Master The Coding Interview – 9 [筆記] Master The Coding Interview – 9](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 8
![[筆記] Master The Coding Interview – 8 [筆記] Master The Coding Interview – 8](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 7
![[筆記] Master The Coding Interview – 7 [筆記] Master The Coding Interview – 7](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 6
![[筆記] Master The Coding Interview – 6 [筆記] Master The Coding Interview – 6](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 4
![[筆記] Master The Coding Interview – 4 [筆記] Master The Coding Interview – 4](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 3
![[筆記] Master The Coding Interview – 3 [筆記] Master The Coding Interview – 3](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 2
![[筆記] Master The Coding Interview – 2 [筆記] Master The Coding Interview – 2](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 1
![[筆記] Master The Coding Interview – 1 [筆記] Master The Coding Interview – 1](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
工程師的面試中,不免俗的會遇見用各種
