https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant/
思路:模拟就完事了。
class Solution {
public:
vector<vector<string>> displayTable(vector<vector<string>>& orders) {
// 桌号->(菜品名称->数量)
map<int,map<string,int>> cnt;
set<string> foods;
for(vector<string>& order:orders)
{
++cnt[stoi(order[1])][order[2]];
foods.insert(order[2]);
}
vector<vector<string>> ans;
vector<string> first(1,"Table");
for(auto& food:foods)
first.push_back(food);
ans.push_back(move(first));
for(auto& ele:cnt)
{
vector<string> tmp(1,to_string(ele.first));
map<string,int> &curcnt=ele.second;
for(auto &food:foods)
{
if(curcnt.count(food))
tmp.push_back(move(to_string(curcnt[food])));
else
tmp.push_back("0");
}
ans.push_back(move(tmp));
}
return ans;
}
};