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

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

题目:

电子表格是一个网格,它有 26 列(从 'A' 到 'Z')和指定数量的 rows。每个单元格可以存储一个 0 到 1 0 5 10^5 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 <= 1 0 3 10^3 103
  2. 0 <= value <= 1 0 5 10^5 105
  3. 公式保证采用 "=X+Y" 格式,其中 X 和 Y 要么是有效的单元格引用,要么是小于等于 1 0 5 10^5 105 的 非负 整数。
  4. 每个单元格引用由一个大写字母 'A' 到 'Z' 和一个介于 1 和 rows 之间的行号组成。
  5. 总共 最多会对 setCellresetCellgetValue 调用 1 0 4 10^4 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);
 */
相关推荐
深圳市方中禾科技11 小时前
FZH1625 LCD 驱动芯片深度评测与实战指南
算法·led
我不会起名字32212 小时前
一天一道算法题(34):回溯法的经典例题(子集)
java·数据结构·python·算法·golang·深度优先·力扣
kyriewen12 小时前
我花3天抓了一个幽灵bug,凶手藏在第4层
前端·javascript·程序员
huang57914714 小时前
哈希算法的抗碰撞机制与安全性增强策略4
算法
mONESY14 小时前
LangGraph.js 从零上手:把 Agent 工作流从一条线变成一张网
javascript
默_笙14 小时前
⚓ AI 的"官方答题卡":withStructuredOutput 与结构化输出的终局之战
前端·javascript
林浩杨_14 小时前
SIGIR 2026|南京大学:Video-GAR:“通过生成 Query 来验证视频语义理解”的生成增强范式
论文阅读·人工智能·算法
Elsa️74614 小时前
算法一周刷题总结
c++·算法
小小程序猴115 小时前
深圳乘路资讯AI培训怎么样?课程靠谱吗?——一套课程可信度评估模型与实证分析
人工智能·算法
云上先途15 小时前
标签化服务适合哪些人?常见适用场景一次讲清
大数据·人工智能·算法·音视频