A spreadsheet is a grid with 26 columns (labeled from 'A' to 'Z') and a given number of rows. Each cell in the spreadsheet can hold an integer value between 0 and 105.
Implement the Spreadsheet class:
Spreadsheet(int rows) Initializes a spreadsheet with 26 columns (labeled 'A' to 'Z') and the specified number of rows. All cells are initially set to 0.
void setCell(String cell, int value) Sets the value of the specified cell. The cell reference is provided in the format "AX" (e.g., "A1", "B10"), where the letter represents the column (from 'A' to 'Z') and the number represents a 1-indexed row.
void resetCell(String cell) Resets the specified cell to 0.
int getValue(String formula) Evaluates a formula of the form "=X+Y", where X and Y are either cell references or non-negative integers, and returns the computed sum.
Note: If getValue references a cell that has not been explicitly set using setCell, its value is considered 0.
Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns
spreadsheet.getValue("=5+7"); // returns 12 (5+7)
spreadsheet.setCell("A1", 10); // sets A1 to 10
spreadsheet.getValue("=A1+6"); // returns 16 (10+6)
spreadsheet.setCell("B2", 15); // sets B2 to 15
spreadsheet.getValue("=A1+B2"); // returns 25 (10+15)
spreadsheet.resetCell("A1"); // resets A1 to 0
spreadsheet.getValue("=A1+B2"); // returns 15 (0+15)
Constraints:
1 <= rows <= 103
0 <= value <= 105
The formula is always in the format "=X+Y", where X and Y are either valid cell references or non-negative integers with values less than or equal to 105.
Each cell reference consists of a capital letter from 'A' to 'Z' followed by a row number between 1 and rows.
At most 104 calls will be made in total to setCell, resetCell, and getValue.
Solutions
Solution 1: Hash Table
We use a hash table \(\textit{d}\) to record the values of all cells, where the key is the cell reference and the value is the cell's value.
When calling the setCell method, we store the cell reference and value in the hash table.
When calling the resetCell method, we remove the cell reference from the hash table.
When calling the getValue method, we split the formula into two cell references, calculate their values, and then return their sum.
The time complexity is \(O(L)\), and the space complexity is \(O(L)\). Where \(L\) is the length of the formula.
1 2 3 4 5 6 7 8 91011121314151617181920212223
classSpreadsheet:def__init__(self,rows:int):self.d={}defsetCell(self,cell:str,value:int)->None:self.d[cell]=valuedefresetCell(self,cell:str)->None:self.d.pop(cell,None)defgetValue(self,formula:str)->int:ans=0forcellinformula[1:].split("+"):ans+=int(cell)ifcell[0].isdigit()elseself.d.get(cell,0)returnans# 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)
classSpreadsheet{privateMap<String,Integer>d=newHashMap<>();publicSpreadsheet(introws){}publicvoidsetCell(Stringcell,intvalue){d.put(cell,value);}publicvoidresetCell(Stringcell){d.remove(cell);}publicintgetValue(Stringformula){intans=0;for(Stringcell:formula.substring(1).split("\\+")){ans+=Character.isDigit(cell.charAt(0))?Integer.parseInt(cell):d.getOrDefault(cell,0);}returnans;}}/** * Your Spreadsheet object will be instantiated and called as such: * Spreadsheet obj = new Spreadsheet(rows); * obj.setCell(cell,value); * obj.resetCell(cell); * int param_3 = obj.getValue(formula); */
classSpreadsheet{private:unordered_map<string,int>d;public:Spreadsheet(introws){}voidsetCell(stringcell,intvalue){d[cell]=value;}voidresetCell(stringcell){d.erase(cell);}intgetValue(stringformula){intans=0;stringstreamss(formula.substr(1));stringcell;while(getline(ss,cell,'+')){if(isdigit(cell[0])){ans+=stoi(cell);}else{ans+=d.count(cell)?d[cell]:0;}}returnans;}};/** * Your Spreadsheet object will be instantiated and called as such: * Spreadsheet* obj = new Spreadsheet(rows); * obj->setCell(cell,value); * obj->resetCell(cell); * int param_3 = obj->getValue(formula); */
typeSpreadsheetstruct{dmap[string]int}funcConstructor(rowsint)Spreadsheet{returnSpreadsheet{d:make(map[string]int)}}func(this*Spreadsheet)SetCell(cellstring,valueint){this.d[cell]=value}func(this*Spreadsheet)ResetCell(cellstring){delete(this.d,cell)}func(this*Spreadsheet)GetValue(formulastring)int{ans:=0cells:=strings.Split(formula[1:],"+")for_,cell:=rangecells{ifval,err:=strconv.Atoi(cell);err==nil{ans+=val}else{ans+=this.d[cell]}}returnans}/** * Your Spreadsheet object will be instantiated and called as such: * obj := Constructor(rows); * obj.SetCell(cell,value); * obj.ResetCell(cell); * param_3 := obj.GetValue(formula); */
classSpreadsheet{privated:Map<string,number>;constructor(rows:number){this.d=newMap<string,number>();}setCell(cell:string,value:number):void{this.d.set(cell,value);}resetCell(cell:string):void{this.d.delete(cell);}getValue(formula:string):number{letans=0;constcells=formula.slice(1).split('+');for(constcellofcells){ans+=isNaN(Number(cell))?this.d.get(cell)||0:Number(cell);}returnans;}}/** * Your Spreadsheet object will be instantiated and called as such: * var obj = new Spreadsheet(rows) * obj.setCell(cell,value) * obj.resetCell(cell) * var param_3 = obj.getValue(formula) */