当前位置: 首页 > news >正文

[LeetCode] 3484. Design Spreadsheet

A spreadsheet is a grid with 26 columns (labeled from 'A' to 'Z') and a given number of rows. Each cell in the spreadsheet can hold an integer value between 0 and 105.

Implement the Spreadsheet class:

Spreadsheet(int rows) Initializes a spreadsheet with 26 columns (labeled 'A' to 'Z') and the specified number of rows. All cells are initially set to 0.
void setCell(String cell, int value) Sets the value of the specified cell. The cell reference is provided in the format "AX" (e.g., "A1", "B10"), where the letter represents the column (from 'A' to 'Z') and the number represents a 1-indexed row.
void resetCell(String cell) Resets the specified cell to 0.
int getValue(String formula) Evaluates a formula of the form "=X+Y", where X and Y are either cell references or non-negative integers, and returns the computed sum.
Note: If getValue references a cell that has not been explicitly set using setCell, its value is considered 0.

Example 1:
Input:
["Spreadsheet", "getValue", "setCell", "getValue", "setCell", "getValue", "resetCell", "getValue"]
[[3], ["=5+7"], ["A1", 10], ["=A1+6"], ["B2", 15], ["=A1+B2"], ["A1"], ["=A1+B2"]]

Output:
[null, 12, null, 16, null, 25, null, 15]

Explanation
Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns
spreadsheet.getValue("=5+7"); // returns 12 (5+7)
spreadsheet.setCell("A1", 10); // sets A1 to 10
spreadsheet.getValue("=A1+6"); // returns 16 (10+6)
spreadsheet.setCell("B2", 15); // sets B2 to 15
spreadsheet.getValue("=A1+B2"); // returns 25 (10+15)
spreadsheet.resetCell("A1"); // resets A1 to 0
spreadsheet.getValue("=A1+B2"); // returns 15 (0+15)

Constraints:
1 <= rows <= 103
0 <= value <= 105
The formula is always in the format "=X+Y", where X and Y are either valid cell references or non-negative integers with values less than or equal to 105.
Each cell reference consists of a capital letter from 'A' to 'Z' followed by a row number between 1 and rows.
At most 104 calls will be made in total to setCell, resetCell, and getValue.

设计电子表格。

电子表格是一个网格,它有 26 列(从 'A' 到 'Z')和指定数量的 rows。每个单元格可以存储一个 0 到 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 。

思路

思路是用 HashMap<String, Integer> 存储“被设置过”的单元格,没出现过的默认值 0。getValue 只需把 =X+Y 去掉 = 后按 + 切分,再分别判断是数字还是单元格引用。

在 getValue 方法里,我们把 =X+Y 切成了两个 token。每个 token 要么是纯数字(比如 "3"、"100"),要么是单元格名字(比如 "A1"、"B12")。

复杂度

时间O(1)
空间O(n) - 存储过多少个数字

代码

Java实现

class Spreadsheet {HashMap<String, Integer> map = new HashMap<>();public Spreadsheet(int rows) {}public void setCell(String cell, int value) {map.put(cell, value);}public void resetCell(String cell) {map.remove(cell);}public int getValue(String formula) {int res = 0;for (String cell : formula.substring(1).split("\\+")) {// System.out.println(cell);if (Character.isUpperCase(cell.charAt(0))) {res += map.getOrDefault(cell, 0);} else {res += Integer.valueOf(cell);}}return res;}
}/*** 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);*/
http://www.jsqmd.com/news/271/

相关文章:

  • Redis的使用问题
  • AIGC拾遗:Flash Attention
  • 深度好文-风雨飘摇信竞路
  • Python-CSV库
  • C++小白修仙记_LeetCode刷题_位运算
  • C++小白修仙记_LeetCode刷题_双指针
  • 前路漫漫亦灿灿 往事堪堪亦澜澜
  • 设计模式(C++)详解—单例模式(2) - 指南
  • 使用uv和pycharm搭建python开发环境
  • lc1032-字符流
  • 八股整理xdsm - 教程
  • C++小白修仙记_LeetCode刷题_哈希表
  • 【F#学习】字符串String
  • US$98 Yanhua Mini ACDP Module4 BMW 35080, 35160DO WT EEPROM Read Write
  • US$98 Yanhua Mini ACDP Module4 BMW 35080, 35160DO WT EEPROM Read Write
  • 深入解析:K8s学习笔记(二) Pod入门与实战
  • 现代汽车前瞻杯2025牛客暑期多校训练营3
  • 详细介绍:[新启航]白光干涉仪在微透镜阵列微观 3D 轮廓测量中的应用解析
  • 实用指南:多技术融合提升环境生态水文、土地土壤、农业大气等领域的数据分析与项目科研水平
  • 【F#学习】“变量”?绑定!
  • 2023 CCPC 深圳 F
  • 完整教程:【算法】双指针(三)[快慢指针]-快乐数
  • 9.19做题资料:哈希表查找时间复杂度分析
  • CF2143F Increasing Xor
  • 提到链接,你能想到什么
  • 实用指南:容器逃逸漏洞
  • 实用指南:容器逃逸漏洞
  • 深入解析:卷对卷(Roll-to-Roll,R2R)技术的应用领域和技术进展
  • 三种方式处理SpringBoot全局异常
  • 解题记录说是 | P3695 CYaRon!语