当前位置:网站首页>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;
}
};
边栏推荐
- 使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
- B站带货当学新东方
- Intelligent fish tank control system based on STM32 under Internet of things
- VSCode无网环境快速迁移开发环境(VIP典藏版)
- 虚拟货币7个月蒸发2万亿美元,“马斯克们”终结15万人暴富梦
- 煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
- Tdengine can read and write through dataX
- memcached全面剖析–2. 理解memcached的內存存儲
- 自己总结的wireshark抓包技巧
- 【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
猜你喜欢

123. the best time to buy and sell shares III

Shengzhe technology AI intelligent drowning prevention service launched
Visit Amazon memorydb and build your own redis memory database

What does CTO (technical director) usually do?

The first day of handwritten RPC -- review of some basic knowledge

基于STM32的物联网下智能化养鱼鱼缸控制控制系统

CondaValueError: The target prefix is the base prefix. Aborting.

VirtualBox virtual machine installation win10 Enterprise Edition
![[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial](/img/0f/e0b261496d04ca3da8a7d7d19e5bf1.png)
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
随机推荐
[Web Security Basics] some details
Web project deployment
Mysql优化查询速度
Golang reflection operation collation
About transform InverseTransformPoint, transform. InverseTransofrmDirection
Unity about conversion between local and world coordinates
TCP Jprobe utilization problem location
Alibaba cloud schedules tasks and automatically releases them
Slider controls the playback progress of animator animation
Dijkstra seeking secondary short circuit (easy to understand)
Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
Implementation of adjacency table storage array of graph
Poj1061 frog dating (extended Euclid)
虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
Antdb database online training has started! More flexible, professional and rich
Golang daily question
Multi view function in blender
Volcano成Spark默认batch调度器
TCP_ Nodelay and TCP_ CORK