class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
//of valid j's such that j != i and nums[j] < nums[i]. 比当前值小的有多少个?
map<int, int> cnt;
for(int i=0;i<nums.size();i++){
int value = nums[i];
if (cnt.find(value)==cnt.end()){
cnt[value] = 1;
}
else{
cnt[value] ++;
}
}
int presum = 0;
for(auto it=cnt.begin();it!=cnt.end();it++){
int key = it->first;
int value = it->second; // count
cnt[key] = presum;
presum += value;
}
vector<int> res;
for(int i=0;i<nums.size();i++){
res.push_back(cnt[nums[i]]);
}
return res;
}
};
1365. How Many Numbers Are Smaller Than the Current Number
最新推荐文章于 2021-05-05 17:11:59 发布