【一看就会一写就废 指间算法】设计电子表格 —— 哈希表、字符串处理

指尖划过的轨迹,藏着最细腻的答案~

题目:

电子表格是一个网格,它有 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. 1 <= rows <= <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 3 10^3 </math>103
  2. 0 <= value <= <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 5 10^5 </math>105
  3. 公式保证采用 "=X+Y" 格式,其中 X 和 Y 要么是有效的单元格引用,要么是小于等于 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 5 10^5 </math>105 的 非负 整数。
  4. 每个单元格引用由一个大写字母 'A' 到 'Z' 和一个介于 1 和 rows 之间的行号组成。
  5. 总共 最多会对 setCellresetCellgetValue 调用 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 4 10^4 </math>104 次。

分析:

本题翻译过来就是在一个rows26列的数组中增删该查 ,那我们直接定义一个rows26列的数组就好了呀,这样就可以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);
 */
相关推荐
拉不动的猪5 小时前
移动端调试工具VConsole初始化时的加载阻塞问题
前端·javascript·微信小程序
大金乄8 小时前
封装一个vue2的elementUI 表格组件(包含表格编辑以及多级表头)
前端·javascript
Lee川10 小时前
解锁 JavaScript 的灵魂:深入浅出原型与原型链
javascript·面试
swipe10 小时前
从原理到手写:彻底吃透 call / apply / bind 与 arguments 的底层逻辑
前端·javascript·面试
CoovallyAIHub11 小时前
Moonshine:比 Whisper 快 100 倍的端侧语音识别神器,Star 6.6K!
深度学习·算法·计算机视觉
CoovallyAIHub12 小时前
速度暴涨10倍、成本暴降6倍!Mercury 2用扩散取代自回归,重新定义LLM推理速度
深度学习·算法·计算机视觉
CoovallyAIHub12 小时前
实时视觉AI智能体框架来了!Vision Agents 狂揽7K Star,延迟低至30ms,YOLO+Gemini实时联动!
算法·架构·github
CoovallyAIHub12 小时前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github
CoovallyAIHub12 小时前
OpenClaw:从“19万星标”到“行业封杀”,这只“赛博龙虾”究竟触动了谁的神经?
算法·架构·github