1 Star 0 Fork 0

catyMap / AlgorithmNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Excel.go 1.85 KB
一键复制 编辑 原始数据 按行查看 历史
dogemap 提交于 2022-01-22 21:52 . 更新题库
package main
import (
"fmt"
"strconv"
"strings"
)
type Excel struct {
graph [][]int
laxs [][][]string
}
func Constructor(height int, width byte) Excel {
graph := make([][]int, height+1, height+1)
laxs := make([][][]string, height+1, height+1)
wid := width - 'A' + 1
for i := range graph {
graph[i] = make([]int, wid, wid)
laxs[i] = make([][]string, wid, wid)
for j := 0; j < int(wid); j++ {
laxs[i][j] = make([]string, 1, 1)
}
}
return Excel{
graph: graph,
laxs: laxs,
}
}
func (this *Excel) Set(row int, column byte, val int) {
this.graph[row][column-'A'] = val
this.laxs[row][column-'A'] = []string{}
}
func (this *Excel) Get(row int, column byte) int {
if len(this.laxs[row][column-'A'][0]) == 0 {
return this.graph[row][column-'A']
}
return this.Sum(row, column, this.laxs[row][column-'A'])
}
func (this *Excel) Sum(row int, column byte, numbers []string) int {
val := 0
for _, s := range numbers {
if !strings.Contains(s, ":") {
r, c := str2val(s)
val += this.graph[r][c]
} else {
val += this.strSumVal(s)
}
}
this.graph[row][column-'A'] = val
this.laxs[row][column-'A'] = numbers
return val
}
func str2val(s string) (row, col int) {
word, dig := s[:1][0], s[1:]
row, _ = strconv.Atoi(dig)
return row, int(word - 'A')
}
func (this *Excel) strSumVal(s string) (val int) {
ss := strings.Split(s, ":")
s1, s2 := ss[0], ss[1]
r1, c1 := str2val(s1)
r2, c2 := str2val(s2)
for i := r1; i <= r2; i++ {
for j := c1; j <= c2; j++ {
val += this.graph[i][j]
}
}
return val
}
func main() {
//ex := Constructor(3, 'C')
//ex.Set(1, 'A', 2)
//fmt.Println(ex.Sum(3, 'C', []string{"A1", "A1:B2"}))
//ex.Set(2, 'B', 2)
//fmt.Println(ex.Get(3, 'C'))
ex := Constructor(3, 'C')
fmt.Println(ex.Sum(1, 'A', []string{"A2"}))
ex.Set(2, 'A', 1)
fmt.Println(ex.Sum(3, 'A', []string{"A1"}))
fmt.Println(ex.Get(1, 'A'))
}
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助