Skip to content

Commit df4288a

Browse files
authored
Merge pull request #1278 from 0xff-dev/3484
Add solution and test-cases for problem 3484
2 parents ef64f24 + f402af0 commit df4288a

File tree

3 files changed

+118
-28
lines changed

3 files changed

+118
-28
lines changed

leetcode/3401-3500/3484.Design-Spreadsheet/README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [3484.Design Spreadsheet][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
75

8-
**Example 1:**
6+
Implement the `Spreadsheet` class:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
- `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.
9+
- `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.
10+
- `void resetCell(String cell)` Resets the specified cell to 0.
11+
- `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.
1412

15-
## 题意
16-
> ...
13+
**Note**: If `getValue` references a cell that has not been explicitly set using `setCell`, its value is considered 0.
1714

18-
## 题解
15+
**Example 1:**
1916

20-
### 思路1
21-
> ...
22-
Design Spreadsheet
23-
```go
2417
```
25-
18+
Input:
19+
["Spreadsheet", "getValue", "setCell", "getValue", "setCell", "getValue", "resetCell", "getValue"]
20+
[[3], ["=5+7"], ["A1", 10], ["=A1+6"], ["B2", 15], ["=A1+B2"], ["A1"], ["=A1+B2"]]
21+
22+
Output:
23+
[null, 12, null, 16, null, 25, null, 15]
24+
25+
Explanation
26+
27+
Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns
28+
spreadsheet.getValue("=5+7"); // returns 12 (5+7)
29+
spreadsheet.setCell("A1", 10); // sets A1 to 10
30+
spreadsheet.getValue("=A1+6"); // returns 16 (10+6)
31+
spreadsheet.setCell("B2", 15); // sets B2 to 15
32+
spreadsheet.getValue("=A1+B2"); // returns 25 (10+15)
33+
spreadsheet.resetCell("A1"); // resets A1 to 0
34+
spreadsheet.getValue("=A1+B2"); // returns 15 (0+15)
35+
```
2636

2737
## 结语
2838

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
type Spreadsheet struct {
9+
rows int
10+
sheet [26][]int
11+
}
12+
13+
func Constructor(rows int) Spreadsheet {
14+
sheet := [26][]int{}
15+
for i := range 26 {
16+
sheet[i] = make([]int, rows)
17+
}
18+
return Spreadsheet{
19+
rows: rows,
20+
sheet: sheet,
21+
}
22+
}
23+
24+
func (this *Spreadsheet) SetCell(cell string, value int) {
25+
row := int(cell[0] - 'A')
26+
v, _ := strconv.Atoi(cell[1:])
27+
this.sheet[row][v-1] = value
28+
}
29+
30+
func (this *Spreadsheet) ResetCell(cell string) {
31+
row := int(cell[0] - 'A')
32+
v, _ := strconv.Atoi(cell[1:])
33+
this.sheet[row][v-1] = 0
34+
}
35+
36+
func (this *Spreadsheet) part(str string) int {
37+
index := 0
38+
isNumber := true
39+
if !(str[0] >= '0' && str[0] <= '9') {
40+
isNumber = false
41+
index++
42+
}
43+
num, _ := strconv.Atoi(str[index:])
44+
if isNumber {
45+
return num
46+
}
47+
return this.sheet[int(str[0]-'A')][num-1]
48+
}
49+
func (this *Spreadsheet) GetValue(formula string) int {
50+
left := formula[1:]
51+
parts := strings.Split(left, "+")
52+
a, b := this.part(parts[0]), this.part(parts[1])
53+
return a + b
54+
}
55+
56+
type opt struct {
57+
name string
58+
arg1 string
59+
arg2 int
60+
}
61+
62+
func Solution(rows int, opts []opt) []int {
63+
c := Constructor(rows)
64+
ans := make([]int, 0)
65+
for _, o := range opts {
66+
if o.name == "get" {
67+
ans = append(ans, c.GetValue(o.arg1))
68+
continue
69+
}
70+
if o.name == "set" {
71+
c.SetCell(o.arg1, o.arg2)
72+
continue
73+
}
74+
c.ResetCell(o.arg1)
75+
// reset
76+
}
77+
return ans
578
}

leetcode/3401-3500/3484.Design-Spreadsheet/Solution_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
rows int
14+
opts []opt
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 3, []opt{
18+
{"get", "=5+7", 0},
19+
{"set", "A1", 10},
20+
{"get", "=A1+6", 0},
21+
{"set", "B2", 15},
22+
{"get", "=A1+B2", 0},
23+
{"reset", "A1", 0},
24+
{"get", "=A1+B2", 0},
25+
}, []int{12, 16, 25, 15}},
1926
}
2027

2128
// 开始测试
2229
for i, c := range cases {
2330
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
31+
got := Solution(c.rows, c.opts)
2532
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
34+
c.expect, got, c.rows, c.opts)
2835
}
2936
})
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}

0 commit comments

Comments
 (0)