当前位置:网站首页>leetcode_1365
leetcode_1365
2022-06-24 19:25:00 【programing菜鸟】
:法一
利用条件每个nums[i] <= 100,可以创建一个101空间的频率数组,利用计数排序算出每个数字出现的频率。然后小于当前数字的数字个数就是频率数组中以该数字为下标的前面的所有项数和。
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int> cnt(101);
for(int e : nums){
//频率数组
cnt[e]++;
}
int sum = 0;
vector<int> smallerThatVector(101);
for(int i = 0; i < 101; ++i){
smallerThatVector[i] += sum; //搞出一个前几项和数组
sum += cnt[i];
}
for(int i = 0; i < nums.size(); ++i){
nums[i] = smallerThatVector[nums[i]]; //和数组的项就是答案
}
return nums;
}
};
法二:
利用map的自动排序。创建一棵map,key是int,value是vector。将nums[i]中的每个元素插入map中,且将nums[i]的下表插入对应的vector中。这样再遍历一遍map,ans数组的对应项就是map中的vector前几项的size和。
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
map<int, vector<int>> cnt; //每错,map的value是vector<int>
for(int i = 0; i < nums.size(); ++i){
cnt[nums[i]].push_back(i); //将nums[i]全部insert进入map
} //且将nums[i]对应的下标插入对应的vector<int>
vector<int> ans(nums.size()); //结果数组
int sum = 0;
for(auto it = cnt.begin(); it != cnt.end(); ++it){
int size = (it->second).size();
for(int i = 0; i < size; ++i){
int index = (it->second)[i]; //拿到vector<int>的值,也就是该
ans[index] = sum; //key在原数组对应的下标。
}
sum += (it->second).size(); //sum就是所有小于当前key的个数和
}
return ans;
}
};
边栏推荐
- Three more days
- 介绍BootLoader、PM、kernel和系统开机的总体流程
- What does CTO (technical director) usually do?
- [cloud native learning notes] kubernetes Foundation
- Football information query system based on C language course report + project source code + demo ppt+ project screenshot
- Volcano becomes spark default batch scheduler
- Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
- Minimum cost and maximum flow (template question)
- Why are life science enterprises on the cloud in succession?
- Bld3 getting started UI
猜你喜欢
Why are life science enterprises on the cloud in succession?
Multi view function in blender
Volcano成Spark默认batch调度器
关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
Pod lifecycle in kubernetes
Remove the screen recording reminder (seven cattle cloud demo)
Return of missing persons
Address mapping of virtual memory paging mechanism
Web project deployment
memcached全面剖析–2. 理解memcached的内存存储
随机推荐
Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
memcached全面剖析–2. 理解memcached的内存存储
Alibaba cloud lightweight servers open designated ports
WMI and PowerShell get TCP connection list
[cloud native learning notes] learn about kubernetes configuration list yaml file
Php-pdo parameter binding problem
一文理解OpenStack网络
[cloud native learning notes] deploy applications through yaml files
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
Introduction to interval DP
SYSCALL_ Define5 setsockopt code flow
Pattern recognition - 9 Decision tree
Analysis of BBR congestion control state machine
CondaValueError: The target prefix is the base prefix. Aborting.
Axi DMA IP core operation process
Oauth1.0 introduction
虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
Mysql优化查询速度
Pod lifecycle in kubernetes
(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识