diff --git a/leetcode/3401-3500/3484.Design-Spreadsheet/README.md b/leetcode/3401-3500/3484.Design-Spreadsheet/README.md index 37eb8c215..c99fa4ca6 100755 --- a/leetcode/3401-3500/3484.Design-Spreadsheet/README.md +++ b/leetcode/3401-3500/3484.Design-Spreadsheet/README.md @@ -1,28 +1,38 @@ # [3484.Design Spreadsheet][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. -**Example 1:** +Implement the `Spreadsheet` class: -``` -Input: a = "11", b = "1" -Output: "100" -``` +- `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:** -### 思路1 -> ... -Design Spreadsheet -```go ``` - +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) +``` ## 结语 diff --git a/leetcode/3401-3500/3484.Design-Spreadsheet/Solution.go b/leetcode/3401-3500/3484.Design-Spreadsheet/Solution.go index d115ccf5e..87a2d3820 100644 --- a/leetcode/3401-3500/3484.Design-Spreadsheet/Solution.go +++ b/leetcode/3401-3500/3484.Design-Spreadsheet/Solution.go @@ -1,5 +1,78 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "strconv" + "strings" +) + +type Spreadsheet struct { + rows int + sheet [26][]int +} + +func Constructor(rows int) Spreadsheet { + sheet := [26][]int{} + for i := range 26 { + sheet[i] = make([]int, rows) + } + return Spreadsheet{ + rows: rows, + sheet: sheet, + } +} + +func (this *Spreadsheet) SetCell(cell string, value int) { + row := int(cell[0] - 'A') + v, _ := strconv.Atoi(cell[1:]) + this.sheet[row][v-1] = value +} + +func (this *Spreadsheet) ResetCell(cell string) { + row := int(cell[0] - 'A') + v, _ := strconv.Atoi(cell[1:]) + this.sheet[row][v-1] = 0 +} + +func (this *Spreadsheet) part(str string) int { + index := 0 + isNumber := true + if !(str[0] >= '0' && str[0] <= '9') { + isNumber = false + index++ + } + num, _ := strconv.Atoi(str[index:]) + if isNumber { + return num + } + return this.sheet[int(str[0]-'A')][num-1] +} +func (this *Spreadsheet) GetValue(formula string) int { + left := formula[1:] + parts := strings.Split(left, "+") + a, b := this.part(parts[0]), this.part(parts[1]) + return a + b +} + +type opt struct { + name string + arg1 string + arg2 int +} + +func Solution(rows int, opts []opt) []int { + c := Constructor(rows) + ans := make([]int, 0) + for _, o := range opts { + if o.name == "get" { + ans = append(ans, c.GetValue(o.arg1)) + continue + } + if o.name == "set" { + c.SetCell(o.arg1, o.arg2) + continue + } + c.ResetCell(o.arg1) + // reset + } + return ans } diff --git a/leetcode/3401-3500/3484.Design-Spreadsheet/Solution_test.go b/leetcode/3401-3500/3484.Design-Spreadsheet/Solution_test.go index 14ff50eb4..dd87b6b5d 100644 --- a/leetcode/3401-3500/3484.Design-Spreadsheet/Solution_test.go +++ b/leetcode/3401-3500/3484.Design-Spreadsheet/Solution_test.go @@ -10,30 +10,37 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + rows int + opts []opt + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, []opt{ + {"get", "=5+7", 0}, + {"set", "A1", 10}, + {"get", "=A1+6", 0}, + {"set", "B2", 15}, + {"get", "=A1+B2", 0}, + {"reset", "A1", 0}, + {"get", "=A1+B2", 0}, + }, []int{12, 16, 25, 15}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.rows, c.opts) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.rows, c.opts) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }