作者enmeitiryous (enmeitiryous)
標題Re: [閒聊] 每日leetcode
時間2024-07-22 08:41:23
2418 sort the people
給定兩個array:names和heights,heights[i]是names[i]的對應身高,回傳根據身高排序
由高到矮的名字array
思路:根據身高倒序sort回傳配對人名或是用身高當索引的map依序回傳人名再顛倒
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
int n=heights.size();
map<int,string> dd;
vector<string> ans;
for(int i=0;i<n;++i){
dd[heights[i]]=names[i];
}
for(auto j: dd){
ans.push_back(j.second);
}
reverse(ans.begin(),ans.end());
return ans;
--
※ 發信站: 批踢踢實業坊(ptt-web.org.tw), 來自: 36.227.202.239 (臺灣)
※ 文章網址: https://ptt-web.org.tw/Marginalman/M.1721608885.A.AC3