leetcode-1418-点菜展示表
Posted 真不知道叫啥好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-1418-点菜展示表相关的知识,希望对你有一定的参考价值。
题目描述:
自己的提交:
class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: table = set() table_l = [] food = set() food_l = [] for order in orders: if order[1] not in table: table.add(order[1]) table_l.append(order[1]) if order[2] not in food: food.add(order[2]) food_l.append(order[2]) table_l.sort(key=lambda x:int(x) ) food_l.sort() m,n = len(table_l)+1,len(food_l)+1 res = [["0"] * n for _ in range(m)] res[0] = ["Table"] + food_l for i in range(len(table_l)): res[i + 1][0] = table_l[i] dic_food = {} dic_table = {} for i,v in enumerate (food_l): dic_food[v] = i + 1 for i,v in enumerate (table_l): dic_table[v] = i + 1 # print(dic_food) # print(res) for order in orders: res[dic_table[order[1]]][dic_food[order[2]]] = str(int(res[dic_table[order[1]]][dic_food[order[2]]]) + 1) return res
另:
class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: fs = set() ts = collections.defaultdict(lambda :collections.defaultdict(int)) for _, t, f in orders: fs.add(f) ts[int(t)][f] += 1 res = [] res.append([\'Table\']) for f in sorted(list(fs)): res[0].append(f) for t in sorted(list(ts.keys())): res.append([str(t)]) for i in range(1, len(res[0])): res[-1].append(str(ts[t][res[0][i]])) return res
以上是关于leetcode-1418-点菜展示表的主要内容,如果未能解决你的问题,请参考以下文章