int arr[][];
int rows = 0;
public Spreadsheet(int rows) {
arr = new int[rows + 1][26];
this.rows = rows + 1;
}
public void setCell(String cell, int value) {
int col = cell.charAt(0) - 'A';
int row = Integer.valueOf(cell.substring(1));
if (row >= rows) {
return;
}
arr[row][col] = value;
}
public void resetCell(String cell) {
int col = cell.charAt(0) - 'A';
int row = Integer.valueOf(cell.substring(1));
if (row >= rows) {
return;
}
arr[row][col] = 0;
}
public int getValue(String formula) {
//=A1+B2
//=5+7
int equalIndex = formula.indexOf('=');
int plusIndex = formula.indexOf('+');
String op1 = formula.substring(equalIndex + 1, plusIndex);
String op2 = formula.substring(plusIndex + 1);
char char1 = op1.charAt(0);
int val1 = 0;
if (char1 >= 'A' && char1 <= 'Z') {
int row1 = Integer.valueOf(op1.substring(1));
if (row1 >= rows) {
return 0;
}
val1 = arr[row1][char1 - 'A'];
} else {
val1 = Integer.valueOf(op1);
}
char char2 = op2.charAt(0);
int val2 = 0;
if (char2 >= 'A' && char2 <= 'Z') {
int row2 = Integer.valueOf(op2.substring(1));
if (row2 >= rows) {
return 0;
}
val2 = arr[row2][char2 - 'A'];
} else {
val2 = Integer.valueOf(op2);
}
return val1 + val2;
}
}
/**
* 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);
*/