Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (JerryChung)時間1年前 (2024/07/22 13:20), 1年前編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串543/1548 (看更多)
https://leetcode.com/problems/sort-the-people 2418. Sort the People ※ 引述《enmeitiryous (enmeitiryous)》之銘言: : 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; Python Code: class Solution: def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: return [names[i] for i, _ in sorted(enumerate(heights), key=lambda h: h[1], reverse=True)] 剩我一開始只想到enumerate了 不過zip還是比較好看 一二三四五 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.52.67 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721625613.A.AFC.html ※ 編輯: JerryChungYC (60.251.52.67 臺灣), 07/22/2024 13:20:58

07/22 13:23, 1年前 , 1F
one liner大神
07/22 13:23, 1F
文章代碼(AID): #1cdUmDhy (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cdUmDhy (Marginalman)