指尖划过的轨迹,藏着最细腻的答案~
题目:
电子表格是一个网格,它有 26 列(从 'A' 到 'Z')和指定数量的 rows。每个单元格可以存储一个 0 到 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 5 10^5 </math>105 之间的整数值。
请你实现一个 Spreadsheet 类:
- Spreadsheet(int rows)初始化一个具有 26 列(从 'A' 到 'Z')和指定行数的电子表格。所有单元格最初的值都为 0 。
- void setCell(String cell, int value)设置指定单元格的值。单元格引用以 "AX" 的格式提供(例如,"A1","B10"),其中字母表示列(从 'A' 到 'Z'),数字表示从 1 开始的行号。
- void resetCell(String cell)重置指定单元格的值为 0 。
- int getValue(String formula)计算一个公式的值,格式为 "=X+Y",其中 X 和 Y 要么 是单元格引用,要么非负整数,返回计算的和。
注意: 如果 getValue 引用一个未通过 setCell 明确设置的单元格,则该单元格的值默认为 0 。
示例 1:
输入:
            
            
              css
              
              
            
          
          ["Spreadsheet", "getValue", "setCell", "getValue", "setCell", "getValue", "resetCell", "getValue"]
[[3], ["=5+7"], ["A1", 10], ["=A1+6"], ["B2", 15], ["=A1+B2"], ["A1"], ["=A1+B2"]]输出:
            
            
              java
              
              
            
          
          [null, 12, null, 16, null, 25, null, 15]解释:
            
            
              java
              
              
            
          
          Spreadsheet spreadsheet = new Spreadsheet(3); // 初始化一个具有 3 行和 26 列的电子表格
spreadsheet.getValue("=5+7"); // 返回 12 (5+7)
spreadsheet.setCell("A1", 10); // 设置 A1 为 10
spreadsheet.getValue("=A1+6"); // 返回 16 (10+6)
spreadsheet.setCell("B2", 15); // 设置 B2 为 15
spreadsheet.getValue("=A1+B2"); // 返回 25 (10+15)
spreadsheet.resetCell("A1"); // 重置 A1 为 0
spreadsheet.getValue("=A1+B2"); // 返回 15 (0+15)提示:
- 1 <= rows <= <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 3 10^3 </math>103
- 0 <= value <= <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 5 10^5 </math>105
- 公式保证采用 "=X+Y" 格式,其中 X 和 Y 要么是有效的单元格引用,要么是小于等于 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 5 10^5 </math>105 的 非负 整数。
- 每个单元格引用由一个大写字母 'A' 到 'Z' 和一个介于 1 和 rows 之间的行号组成。
- 总共 最多会对
setCell、resetCell和getValue调用 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 4 10^4 </math>104 次。
分析:
本题翻译过来就是在一个rows行26列的数组中增删该查 ,那我们直接定义一个rows行26列的数组就好了呀,这样就可以O(1)的进行所有操作了。
AC代码:
            
            
              c++
              
              
            
          
          class Spreadsheet {
    vector<vector<int>> a;
public:
    Spreadsheet(int rows) {
        a.resize(rows + 1);
        for (int i = 0; i <= rows; i ++) {
            a[i].resize(26, 0);
        }
    }
    
    void setCell(string cell, int value) {
        int col = cell[0] - 'A';
        int row = stoi(cell.substr(1));
        a[row][col] = value;
    }
    
    void resetCell(string cell) {
        int col = cell[0] - 'A';
        int row = stoi(cell.substr(1));
        a[row][col] = 0;
    }
    
    int getValue(string formula) {
        int addIdx = formula.find('+');
        string cell1 = formula.substr(1, addIdx - 1);
        string cell2 = formula.substr(addIdx + 1);
        int ans = 0;
        if (isdigit(cell1[0])) {
            ans += stoi(cell1);
        } else {
            int col = cell1[0] - 'A';
            int row = stoi(cell1.substr(1));
            ans += a[row][col];
        }
        if (isdigit(cell2[0])) {
            ans += stoi(cell2);
        } else {
            int col = cell2[0] - 'A';
            int row = stoi(cell2.substr(1));
            ans += a[row][col];
        }
        return ans;
    }
};
/**
 * 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);
 */