2025-09-19 15:44:46 +0000 UTC
Design Spreadsheet
Categories:
Links
Code
class Spreadsheet:
def __init__(self, rows: int):
self._grid = [[0] * 26 for _ in range(rows)]
def _coord(self, cell: str) -> tuple[int, int]:
return int(cell[1:]) - 1, ord(cell[0]) - ord("A")
def setCell(self, cell: str, value: int) -> None:
row, col = self._coord(cell)
self._grid[row][col] = value
def resetCell(self, cell: str) -> None:
row, col = self._coord(cell)
self._grid[row][col] = 0
def getValue(self, formula: str) -> int:
res = 0
for cell in formula[1:].split("+"):
if cell[0].isdigit():
res += int(cell)
else:
row, col = self._coord(cell)
res += self._grid[row][col]
return res
# Your Spreadsheet object will be instantiated and called as such:
# obj = Spreadsheet(rows)
# obj.setCell(cell,value)
# obj.resetCell(cell)
# param_3 = obj.getValue(formula)