Skip to content

Commit 4129157

Browse files
authored
Create 3484. Design Spreadsheet (#889)
2 parents 7868dcb + 7e07ac9 commit 4129157

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

3484. Design Spreadsheet

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
*/

0 commit comments

Comments
 (0)