Re: [閒聊] 每日leetcode

看板Marginalman作者 (smart0eddie)時間1年前 (2024/08/05 14:58), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串641/1548 (看更多)
2024-08-05 2053. Kth Distinct String in an Array A distinct string is a string that is present only once in an array. Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "". Note that the strings are considered in the order in which they appear in the array. 好像除了用 dict 去計數以外我不知道怎麼做欸 class Solution { public: string kthDistinct(vector<string>& arr, int k) { unordered_map<string, int> count; for (auto& s : arr) { ++count[s]; } int curK = 0; for (auto& s : arr) { if (1 == count[s]) { curK++; } if (curK == k) { return s; } } return ""; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 73.173.211.221 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1722841095.A.0E3.html
文章代碼(AID): #1ci7W73Z (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ci7W73Z (Marginalman)