-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignSpreadsheet.java
More file actions
117 lines (96 loc) · 3.02 KB
/
DesignSpreadsheet.java
File metadata and controls
117 lines (96 loc) · 3.02 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package Algorithms.Matrix;
import java.util.HashMap;
import java.util.Map;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 15 March 2025
*/
public class DesignSpreadsheet {
public static void main(String[] args) {
Spreadsheet s = new Spreadsheet(3);
s.setCell("A1", 1);
s.setCell("A2", 2);
s.setCell("A3", 3);
System.out.println("s.getValue(\"A1\") => " + s.getValue("A1+8"));
s.resetCell("A1");
}
}
// https://leetcode.com/problems/design-spreadsheet/
class Spreadsheet {
int[][] sheet;
Spreadsheet(int rows) {
sheet = new int[rows][26];
}
public void setCell(String cell, int value) {
int row = Integer.parseInt(cell.substring(1)) - 1;
int col = cell.charAt(0) - 'A';
sheet[row][col] = value;
}
public void resetCell(String cell) {
int row = Integer.parseInt(cell.substring(1)) - 1;
int col = cell.charAt(0) - 'A';
sheet[row][col] = 0;
}
public int getValue(String formula) {
// formula can be "=A1+B2" or "5+B33"
// return the result
String ls="", rs="";
String s[] = formula.split("\\+");
ls = s[0].substring(1);
rs = s[1];
int lv = 0;
if ( Character.isDigit(ls.charAt(0)) ) lv = Integer.parseInt(ls);
else {
int col = ls.charAt(0) - 'A';
int row = Integer.parseInt(ls.substring(1));
lv=sheet[row][col];
}
int rv = 0;
if ( Character.isDigit(rs.charAt(0)) ) rv = Integer.parseInt(rs);
else {
int col = rs.charAt(0) - 'A';
int row = Integer.parseInt(rs.substring(1));
rv=sheet[row][col];
}
return lv+rv;
}
class SpreadsheetUsingMap {
Map<String, Integer> map;
public SpreadsheetUsingMap(int rows) {
map = new HashMap<>();
}
public void setCell(String cell, int value) {
map.put(cell, value);
}
public void resetCell(String cell) {
map.put(cell, 0);
}
public int getValue(String f) {
int index = -1;
for(int i = 1; i < f.length(); i++) {
if(f.charAt(i) == '+') {
index = i;
break;
}
}
int ans = 0;
char ch = f.charAt(1);
if(ch >= 'A' && ch <= 'Z') {
String sub = f.substring(1, index);
if(map.containsKey(sub)) ans += map.get(sub);
} else {
String sub = f.substring(1, index);
ans += Integer.parseInt(sub);
}
ch = f.charAt(index+1);
if(ch >= 'A' && ch <= 'Z') {
String sub = f.substring(index+1);
if(map.containsKey(sub)) ans += map.get(sub);
} else {
String sub = f.substring(index+1);
ans += Integer.parseInt(sub);
}
return ans;
}
}
}