LeetCode 706. Design HashMap in F#
URL
leetcode.com/problems/design-hashmap
Code
github.com/syohex/dotnet-study/blob/master/..
type MyHashMap =
{ Table: (int * int) list [] }
static member init() : MyHashMap =
{ Table = Array.init 4096 (fun _ -> []) }
static member hash(key: int) : int = key % 4096
member this.put (key: int) (value: int) =
let idx = MyHashMap.hash key
match List.tryFindIndex (fun (k, _) -> k = key) this.Table.[idx] with
| None -> this.Table.[idx] <- ((key, value) :: this.Table.[idx])
| Some (i) -> this.Table.[idx] <- List.updateAt i (key, value) this.Table.[idx]
member this.get(key: int) : int =
let idx = MyHashMap.hash key
match List.tryFind (fun (k, _) -> k = key) this.Table.[idx] with
| None -> -1
| Some ((_, v)) -> v
member this.remove(key: int) =
let idx = MyHashMap.hash key
match List.tryFindIndex (fun (k, _) -> k = key) this.Table.[idx] with
| None -> ()
| Some (i) -> this.Table.[idx] <- List.removeAt i this.Table.[idx]