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

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

题目:

电子表格是一个网格,它有 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);
 */
相关推荐
FanXing_zl7 小时前
基于整数MCU的FOC控制定标策略深度解析
单片机·嵌入式硬件·mcu·算法·定点运算·q15
立志成为大牛的小牛8 小时前
数据结构——三十三、Dijkstra算法(王道408)
数据结构·笔记·学习·考研·算法·图论
地平线开发者9 小时前
mul 与 reduce_sum 的优化实例
算法·自动驾驶
yume_sibai9 小时前
TS 常用内置方法
前端·javascript·typescript
新知图书9 小时前
ArkTS语言、基本组成与数据类型
前端·javascript·typescript
西西学代码9 小时前
Flutter---个人信息(1)---实现简单的UI
开发语言·javascript·flutter
嘗_9 小时前
手写自己的小型react
前端·javascript·react.js
坚持编程的菜鸟9 小时前
LeetCode每日一题——Pow(x, n)
c语言·算法·leetcode
zuo-yiran9 小时前
vue div标签可输入状态下实现数据双向绑定
前端·javascript·vue.js
csdn_aspnet9 小时前
分享MATLAB在数据分析与科学计算中的高效算法案例
算法·matlab·数据分析