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

看板Marginalman作者 (caster )時間1年前 (2024/07/22 09:32), 編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串542/1548 (看更多)
※ 引述《argorok (死肥肥社管)》之銘言: : ※ 引述《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; : ez守門員來了 zip起來根據身高sort再把名字傳回去 : class Solution: : def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: : return [name for _, name in sorted(zip(heights, names), reverse=True)] 思路: zip包起來 sorted zip打開來 回傳 Python Code: class Solution: def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: sorted_pairs = sorted(zip(heights,names), reverse = True) heights, names = zip(*sorted_pairs) return names 我是EZ戰神 口牙 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.160.111 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721611935.A.1E7.html

07/22 09:35, 1年前 , 1F
別卷了,去打手槍
07/22 09:35, 1F

07/22 09:36, 1年前 , 2F
大師
07/22 09:36, 2F
文章代碼(AID): #1cdRQV7d (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cdRQV7d (Marginalman)