Re: [闲聊] 每日LeetCode

楼主: wu10200512 (廷廷)   2024-01-31 22:10:32
为啥我sort的判断函数一定要用静态函数啊
他报错叫我加才加的
有没有人知道
347. Top K Frequent Elements
class Solution {
public:
static bool sortpair(const pair<int, int>& a, const pair<int,int>& b){
return a.second>b.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> mp;
vector<pair<int, int>> v;
for(const int& n:nums){
if(mp.count(n)){
mp[n]++;
}
else{
mp[n]=1;
}
}
for(const auto& p:mp){
v.push_back(p);
}
sort(v.begin(), v.end(), sortpair);
vector<int> ans(k);
for(int i=0; i<k; i++){
ans[i]=v[i].first;
}
return ans;
}
};
作者: chuegou (chuegou)   2024-01-31 22:20:00
因为call by class的限制吗?
作者: HccrtZ (Violet)   2024-01-31 22:26:00
那个error google就有了 没遇过真的没注意到 :OOO
作者: SecondRun (雨夜琴声)   2024-01-31 22:28:00
因为sort是static?
作者: ZooseWu (N5)   2024-01-31 22:28:00
这是JAVA吗
作者: tzyysang (tzyysang)   2024-01-31 22:37:00
简单来说 他要吃的不是function 是functor/comparison function object最方便好读就是写lambda就好= =
作者: SecondRun (雨夜琴声)   2024-01-31 22:39:00
那是不是把comparer移到class外面就可以了
楼主: wu10200512 (廷廷)   2024-01-31 22:45:00
是不是该学lambda了
作者: SecondRun (雨夜琴声)   2024-01-31 22:47:00
lambda必学的

Links booklink

Contact Us: admin [ a t ] ucptt.com