forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpreadsheet.java
More file actions
45 lines (37 loc) · 1.27 KB
/
Spreadsheet.java
File metadata and controls
45 lines (37 loc) · 1.27 KB
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
36
37
38
39
40
41
42
43
44
45
package g3401_3500.s3484_design_spreadsheet;
// #Medium #Array #String #Hash_Table #Matrix #Design
// #2025_03_17_Time_117_ms_(100.00%)_Space_56.71_MB_(100.00%)
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("unused")
public class Spreadsheet {
private final Map<String, Integer> data = new HashMap<>();
public Spreadsheet(int rows) {}
public void setCell(String cell, int value) {
data.put(cell, value);
}
public void resetCell(String cell) {
data.put(cell, 0);
}
public int getValue(String formula) {
int index = formula.indexOf('+');
String left = formula.substring(1, index);
String right = formula.substring(index + 1);
int x =
Character.isLetter(left.charAt(0))
? data.getOrDefault(left, 0)
: Integer.parseInt(left);
int y =
Character.isLetter(right.charAt(0))
? data.getOrDefault(right, 0)
: Integer.parseInt(right);
return x + y;
}
}
/*
* 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);
*/