fork download
  1. class Node:
  2. def __init__(self, key, val):
  3. self.key = key
  4. self.val = val
  5. self.next = None
  6. class MyHashMap(object):
  7.  
  8. def __init__(self):
  9. self.hashmap = []
  10. for i in range(10000):
  11. self.hashmap.append(Node(None,None))
  12.  
  13. def put(self, key, val):
  14. i = key % 10000
  15. head = self.hashmap[i]
  16. cur = head
  17. while cur:
  18. if cur.key == key:
  19. cur.val = val
  20. return
  21. cur = cur.next
  22. newNode = Node(key,val)
  23. newNode.next = head.next
  24. head.next = newNode
  25.  
  26. def get(self, key):
  27. i = key % 10000
  28. head = self.hashmap[i]
  29. cur = head
  30. while cur:
  31. if cur.key == key:
  32. return cur.val
  33. cur = cur.next
  34. return -1
  35. def remove(self, key):
  36. i = key % 10000
  37. head = self.hashmap[i]
  38. cur = head.next
  39. prev = head
  40.  
  41. while cur:
  42. if cur.key == key:
  43. prev.next = cur.next
  44. return
  45. cur = cur.next
  46. prev = prev.next
  47.  
  48.  
  49.  
  50.  
  51. # Your MyHashMap object will be instantiated and called as such:
  52. # obj = MyHashMap()
  53. # obj.put(key,value)
  54. # param_2 = obj.get(key)
  55. # obj.remove(key)
Success #stdin #stdout 0.12s 14128KB
stdin
Standard input is empty
stdout
Standard output is empty