forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-spreadsheet.cpp
More file actions
35 lines (28 loc) · 769 Bytes
/
Copy pathdesign-spreadsheet.cpp
File metadata and controls
35 lines (28 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Time: ctor: O(1)
// setCell: O(1)
// resetCell: O(1)
// getValue: O(1)
// Space: O(n)
// hash table
class Spreadsheet {
public:
Spreadsheet(int rows) {
}
void setCell(string cell, int value) {
lookup_[cell] = value;
}
void resetCell(string cell) {
lookup_.erase(cell);
}
int getValue(string formula) {
const int i = formula.find('+');
const auto& left = formula.substr(1, i - 1);
const auto& right = formula.substr(i + 1);
return value(left) + value(right);
}
private:
int value(const string& k) {
return isalpha(k[0]) ? (lookup_.count(k) ? lookup_[k] : 0) : stoi(k);
}
unordered_map<string, int> lookup_;
};