可以用map 也可以用sort,不过sort必须.begin, .end这么用
struct V {
int x, s;
};
bool comp(V a, V b) {
return a.s > b.s;
}
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int, int> mymap;
for (int i = 0; i < nums.size(); i++) {
mymap[nums[i]]++;
}
vector<V> v;
for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++) {
v.push_back((V){it->first, it->second});
}
sort(v.begin(), v.end(), comp);
vector<int> ans;
for (int i = 0; i < k; i++) {
ans.push_back(v[i].x);
}
return ans;
}
};