|
| 1 | +class Spreadsheet { |
| 2 | + |
| 3 | + private final Map<Character, Map<Integer, Integer>> sheet; |
| 4 | + |
| 5 | + public Spreadsheet(int rows) { |
| 6 | + sheet = new HashMap<>(); |
| 7 | + for (char c = 'A'; c <= 'Z'; c++) { |
| 8 | + sheet.put(c, new HashMap<>()); |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | + public void setCell(String cell, int value) { |
| 13 | + char column = cell.charAt(0); |
| 14 | + int rowNumber = Integer.parseInt(cell.substring(1)); |
| 15 | + Map<Integer, Integer> row = sheet.get(column); |
| 16 | + row.put(rowNumber, value); |
| 17 | + } |
| 18 | + |
| 19 | + public void resetCell(String cell) { |
| 20 | + setCell(cell, 0); |
| 21 | + } |
| 22 | + |
| 23 | + public int getValue(String formula) { |
| 24 | + String[] split = formula.split("\\+"); |
| 25 | + String cellOne = split[0].substring(1); |
| 26 | + String cellTwo = split[1]; |
| 27 | + return getCellValue(cellOne) + getCellValue(cellTwo); |
| 28 | + } |
| 29 | + |
| 30 | + private int getCellValue(String cell) { |
| 31 | + if (!Character.isLetter(cell.charAt(0))) { |
| 32 | + return Integer.parseInt(cell); |
| 33 | + } |
| 34 | + char column = cell.charAt(0); |
| 35 | + int rowNumber = Integer.parseInt(cell.substring(1)); |
| 36 | + Map<Integer, Integer> row = sheet.get(column); |
| 37 | + return row.getOrDefault(rowNumber, 0); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Your Spreadsheet object will be instantiated and called as such: |
| 43 | + * Spreadsheet obj = new Spreadsheet(rows); |
| 44 | + * obj.setCell(cell,value); |
| 45 | + * obj.resetCell(cell); |
| 46 | + * int param_3 = obj.getValue(formula); |
| 47 | + */ |
0 commit comments