关于算法:1418菜谱

43次阅读

共计 618 个字符,预计需要花费 2 分钟才能阅读完成。

func displayTable(orders [][]string) (ret [][]string) {tb := make([]map[string]int, 501)

    title := []string{}
    foodMap := map[string]bool{}

    for _, v := range orders {tbNum, _ := strconv.Atoi(v[1])
        food := v[2]

        if tb[tbNum] == nil {tb[tbNum] = map[string]int{}}

        if !foodMap[food] {title = append(title, food)
            foodMap[food] = true
        }

        tb[tbNum][food]++
    }

    sort.Strings(title)
    title = append([]string{"Table"}, title...)

    ret = append(ret, title)

    for tbNum := 1; tbNum < 501; tbNum++ {if tb[tbNum] == nil {continue}
        row := []string{strconv.Itoa(tbNum)}
        for i := 1; i < len(title); i++ {if x, ok := tb[tbNum][title[i]]; ok {row = append(row, strconv.Itoa(x))
                continue
            }
            row = append(row, "0")
        }
        ret = append(ret, row)
    }
    
    return ret
}

正文完
 0