|
| 1 | +classSpreadsheet { |
| 2 | + |
| 3 | +privatefinalMap<Character,Map<Integer,Integer>>sheet; |
| 4 | + |
| 5 | +publicSpreadsheet(introws) { |
| 6 | +sheet =newHashMap<>(); |
| 7 | +for (charc ='A';c <='Z';c++) { |
| 8 | +sheet.put(c,newHashMap<>()); |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | +publicvoidsetCell(Stringcell,intvalue) { |
| 13 | +charcolumn =cell.charAt(0); |
| 14 | +introwNumber =Integer.parseInt(cell.substring(1)); |
| 15 | +Map<Integer,Integer>row =sheet.get(column); |
| 16 | +row.put(rowNumber,value); |
| 17 | + } |
| 18 | + |
| 19 | +publicvoidresetCell(Stringcell) { |
| 20 | +setCell(cell,0); |
| 21 | + } |
| 22 | + |
| 23 | +publicintgetValue(Stringformula) { |
| 24 | +String[]split =formula.split("\\+"); |
| 25 | +StringcellOne =split[0].substring(1); |
| 26 | +StringcellTwo =split[1]; |
| 27 | +returngetCellValue(cellOne) +getCellValue(cellTwo); |
| 28 | + } |
| 29 | + |
| 30 | +privateintgetCellValue(Stringcell) { |
| 31 | +if (!Character.isLetter(cell.charAt(0))) { |
| 32 | +returnInteger.parseInt(cell); |
| 33 | + } |
| 34 | +charcolumn =cell.charAt(0); |
| 35 | +introwNumber =Integer.parseInt(cell.substring(1)); |
| 36 | +Map<Integer,Integer>row =sheet.get(column); |
| 37 | +returnrow.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 | + */ |