精華區beta Marginalman 關於我們 聯絡資訊
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