mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-07 00:11:41 +08:00
45 lines
3.6 KiB
HTML
45 lines
3.6 KiB
HTML
<p>A spreadsheet is a grid with 26 columns (labeled from <code>'A'</code> to <code>'Z'</code>) and a given number of <code>rows</code>. Each cell in the spreadsheet can hold an integer value between 0 and 10<sup>5</sup>.</p>
|
|
|
|
<p>Implement the <code>Spreadsheet</code> class:</p>
|
|
|
|
<ul>
|
|
<li><code>Spreadsheet(int rows)</code> Initializes a spreadsheet with 26 columns (labeled <code>'A'</code> to <code>'Z'</code>) and the specified number of rows. All cells are initially set to 0.</li>
|
|
<li><code>void setCell(String cell, int value)</code> Sets the value of the specified <code>cell</code>. The cell reference is provided in the format <code>"AX"</code> (e.g., <code>"A1"</code>, <code>"B10"</code>), where the letter represents the column (from <code>'A'</code> to <code>'Z'</code>) and the number represents a <strong>1-indexed</strong> row.</li>
|
|
<li><code>void resetCell(String cell)</code> Resets the specified cell to 0.</li>
|
|
<li><code>int getValue(String formula)</code> Evaluates a formula of the form <code>"=X+Y"</code>, where <code>X</code> and <code>Y</code> are <strong>either</strong> cell references or non-negative integers, and returns the computed sum.</li>
|
|
</ul>
|
|
|
|
<p><strong>Note:</strong> If <code>getValue</code> references a cell that has not been explicitly set using <code>setCell</code>, its value is considered 0.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong><br />
|
|
<span class="example-io">["Spreadsheet", "getValue", "setCell", "getValue", "setCell", "getValue", "resetCell", "getValue"]<br />
|
|
[[3], ["=5+7"], ["A1", 10], ["=A1+6"], ["B2", 15], ["=A1+B2"], ["A1"], ["=A1+B2"]]</span></p>
|
|
|
|
<p><strong>Output:</strong><br />
|
|
<span class="example-io">[null, 12, null, 16, null, 25, null, 15] </span></p>
|
|
|
|
<p><strong>Explanation</strong></p>
|
|
Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns<br data-end="321" data-start="318" />
|
|
spreadsheet.getValue("=5+7"); // returns 12 (5+7)<br data-end="373" data-start="370" />
|
|
spreadsheet.setCell("A1", 10); // sets A1 to 10<br data-end="423" data-start="420" />
|
|
spreadsheet.getValue("=A1+6"); // returns 16 (10+6)<br data-end="477" data-start="474" />
|
|
spreadsheet.setCell("B2", 15); // sets B2 to 15<br data-end="527" data-start="524" />
|
|
spreadsheet.getValue("=A1+B2"); // returns 25 (10+15)<br data-end="583" data-start="580" />
|
|
spreadsheet.resetCell("A1"); // resets A1 to 0<br data-end="634" data-start="631" />
|
|
spreadsheet.getValue("=A1+B2"); // returns 15 (0+15)</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= rows <= 10<sup>3</sup></code></li>
|
|
<li><code>0 <= value <= 10<sup>5</sup></code></li>
|
|
<li>The formula is always in the format <code>"=X+Y"</code>, where <code>X</code> and <code>Y</code> are either valid cell references or <strong>non-negative</strong> integers with values less than or equal to <code>10<sup>5</sup></code>.</li>
|
|
<li>Each cell reference consists of a capital letter from <code>'A'</code> to <code>'Z'</code> followed by a row number between <code>1</code> and <code>rows</code>.</li>
|
|
<li>At most <code>10<sup>4</sup></code> calls will be made in <strong>total</strong> to <code>setCell</code>, <code>resetCell</code>, and <code>getValue</code>.</li>
|
|
</ul>
|