class LRU(object): def __init__(self, size=3): self._size = size self._cache = HashTable() self._keys = LinkedList() def __str__(self): return str(self._keys) def __setitem__(self, key, value): if len(self._keys) < self._size: self._keys.prepend(key) self._cache[key] = value else: k = self._keys.pop(key) if k is None: last_key = self._keys.pop() self._keys.prepend(key) self._cache.pop(last_key) self._cache[key] = value else: self._keys.prepend(key) self._cache[key] = value def __getitem__(self, key): return self._cache[key]
def climbStairs_v2(n): stairs = LinkedList(size=n) stairs[0] = 1 stairs[1] = 2 for i in range(2, n): stairs[i] = stairs[i - 1] + stairs[i - 2] return stairs[-1]
class Queue(object): """Implement stack using LinkedList""" def __init__(self): self._items = LinkedList() def __str__(self): return '[ ' + ' '.join(str(i) for i in self._items) + ' ]' def enqueue(self, item): self._items.prepend(item) def dequeue(self): return self._items.pop() def isEmpty(self): return self._items.is_empty() @property def size(self): return len(self._items)
class Stack(object): """Implement stack using LinkedList""" def __init__(self): self._items = LinkedList() def __str__(self): return '[ ' + ' '.join(str(i) for i in self._items) + ' ]' def push(self, item): self._items.append(item) def pop(self): return self._items.pop() def peek(self): return self._items[-1] def isEmpty(self): return self._items.is_empty() @property def size(self): return len(self._items)
def __init__(self, size=3): self._size = size self._cache = HashTable() self._keys = LinkedList()
def __init__(self): self._items = LinkedList()
def __init__(self, size=13): self._size = size self._slots = [LinkedList() for _ in range(self._size)] self._items = [LinkedList() for _ in range(self._size)]