Re: [閒聊] 每日leetcode
看板Marginalman作者smart0eddie (smart0eddie)時間1年前 (2024/08/05 14:58)推噓0(0推 0噓 0→)留言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
討論串 (同標題文章)
完整討論串 (本文為第 641 之 1548 篇):