File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ class Spreadsheet {
2+ public:
3+ using int2=pair<short, char>;
4+ int A[1000][26];
5+ int rows;
6+ Spreadsheet(int rows) : rows(rows) {
7+ memset(A, 0, rows*26*sizeof(int));
8+ }
9+
10+ int2 idx(const string& cell){
11+ char col=cell[0]-'A';
12+ short row=stoi(cell.substr(1))-1;
13+ return {row, col};
14+ }
15+ void setCell(const string& cell, int value) {
16+ A[idx(cell).first][idx(cell).second]=value;
17+ }
18+
19+ void resetCell(const string& cell) {
20+ A[idx(cell).first][idx(cell).second]=0;
21+ }
22+
23+ int getValue(const string& formula) {
24+ int m=formula.find('+');
25+ int x, y;
26+ const string xx=formula.substr(1, m-1), yy=formula.substr(m+1);
27+ if (formula[1]>='A')
28+ x=A[idx(xx).first][idx(xx).second];
29+ else x=stoi(xx);
30+ if (formula[m+1]>='A')
31+ y=A[idx(yy).first][idx(yy).second];
32+ else y=stoi(yy);
33+ return x+y;
34+ }
35+ };
36+
37+ /**
38+ * Your Spreadsheet object will be instantiated and called as such:
39+ * Spreadsheet* obj = new Spreadsheet(rows);
40+ * obj->setCell(cell,value);
41+ * obj->resetCell(cell);
42+ * int param_3 = obj->getValue(formula);
43+ */
You can’t perform that action at this time.
0 commit comments