[LeetCode] 3484. Design Spreadsheet

news/2025/9/20 0:06:04/文章来源:https://www.cnblogs.com/cnoodle/p/19101891

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.mzph.cn/news/908161.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis的使用问题

1:穿透,访问缓存中没有,数据库中也没有的数据,直接忽略缓存层,直达数据库 解决方案:方案一,查一个不存在的数据时,给一个设置一定过期间的key的数据,存入缓存,方案二:布隆过滤器,在将数据存入Redis时,会同…

AIGC拾遗:Flash Attention

前言 对于attention操作,其计算复杂度随着序列长度的增加呈平方倍的增长。因此,出现了诸多尝试将计算复杂度降低为\(O(n)\)的注意力机制。然而,这些方法忽略了计算时的IO复杂度的影响,频繁的内存交换也在长序列计算…

深度好文-风雨飘摇信竞路

风雨飘摇信竞路 写作时间:2025.9.19夜 1. 引子 夜深了,我捣鼓好了博客园,长舒了一口气。 明天就是 CSP-S 的初赛了,上周老师说可能这次我们没有初赛直升的名额了,把我们搞得都很慌,做了不少卷子。明天早上我还要…

Python-CSV库

CSV (Comma Separated Values) 是电子表格和数据库中最常见的数据交换格式。Python 的 csv 模块提供了读写 CSV 文件的功能,支持多种 CSV 变体和自定义格式。Python CSV 库 1. 库概述 1.1 简介 CSV (Comma Separated …

C++小白修仙记_LeetCode刷题_位运算

位运算 (难度:easy) 231. 2 的幂 给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false 。 如果存在一个整数 x 使得 n == 2x ,则认为 n 是 2 的幂次方。 示例: 输入:n = 1 …

C++小白修仙记_LeetCode刷题_双指针

双指针(easy) 345. 反转字符串中的元音字母 给你一个字符串 s ,仅反转字符串中的所有元音字母,并返回结果字符串。 元音字母包括 a、e、i、o、u,且可能以大小写两种形式出现不止一次。 示例: 输入:s = "Ic…

前路漫漫亦灿灿 往事堪堪亦澜澜

想了好久不知道从何下笔。 谨以本文慰藉我一段难忘的大学生生活,一个难忘的学期。 仍然忘不了军训时了解到ACM实验室,一切的一切从某位舍友,未来的集训队友、假期的守舍人、一个讨厌的人开始。从他那里的得知道了编…

设计模式(C++)详解—单例模式(2) - 指南

设计模式(C++)详解—单例模式(2) - 指南2025-09-19 22:51 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block …

使用uv和pycharm搭建python开发环境

uv是一个Rust编写的极速Python包和项目管理工具。官网: https://docs.astral.sh/uv/ , 中文的详细使用文档: https://uv.doczh.com/ 可以用来安装和管理个多版本python,创建管理不同的虚拟环境,所谓虚拟环境是将包…

lc1032-字符流

难度:困难题目描述设计一个算法:接收一个字符流,并检查每个新字符加进来形成的新串,其后缀是否是字符串数组 words 中的一个字符串示例 输入: ["StreamChecker", "query", "query"…

lc1032-字符流

难度:困难题目描述设计一个算法:接收一个字符流,并检查每个新字符加进来形成的新串,其后缀是否是字符串数组 words 中的一个字符串示例 输入: ["StreamChecker", "query", "query"…

八股整理xdsm - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

C++小白修仙记_LeetCode刷题_哈希表

哈希表(难度:easy) 217. 存在重复元素 给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。 示例: 输入:nums = [1,2,3,1] 输出:true 解释: 元…

【F#学习】字符串String

字符串 F#的字符串和其他现代化的语言的字符串差异不大。 let fruit = "Apple"字符串可以通过调用其本身的函数来修改,也可以通过String模块下的函数来修改——但字符串是常量,一旦被创建就不可能发生改变…

US$98 Yanhua Mini ACDP Module4 BMW 35080, 35160DO WT EEPROM Read Write

Yanhua Mini ACDP Module 4 BMW 35080, 35160DO WT EEPROM Read & WriteNo need soldering.Function:Read and write BMW M35080, 35160DO WT etc EEPROM Yanhua Mini ACDP Module 4 Package includes:Item No. Ad…

US$98 Yanhua Mini ACDP Module4 BMW 35080, 35160DO WT EEPROM Read Write

Yanhua Mini ACDP Module 4 BMW 35080, 35160DO WT EEPROM Read & WriteNo need soldering.Function:Read and write BMW M35080, 35160DO WT etc EEPROM Yanhua Mini ACDP Module 4 Package includes:Item No. Ad…

深入解析:K8s学习笔记(二) Pod入门与实战

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

现代汽车前瞻杯2025牛客暑期多校训练营3

F Flower 题意简化: 有一朵初始有n片花瓣的花,Yuki会按轮次摘花瓣:每轮操作中,她先摘a片花瓣,之后再摘b片花瓣;若剩余花瓣不足,就把剩下的全部摘完。这个过程会持续到所有花瓣被摘完为止。 Yuki的规则是:当且仅…

详细介绍:[新启航]白光干涉仪在微透镜阵列微观 3D 轮廓测量中的应用解析

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

实用指南:多技术融合提升环境生态水文、土地土壤、农业大气等领域的数据分析与项目科研水平

实用指南:多技术融合提升环境生态水文、土地土壤、农业大气等领域的数据分析与项目科研水平pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !impor…