当前位置:网站首页>力扣每日一题-第45天-697. 数组的度
力扣每日一题-第45天-697. 数组的度
2022-08-01 16:58:00 【重邮研究森】
2022.8.1今天你刷题了吗?
题目:
给定一个非空且只包含非负数的整数数组 nums,数组的 度 的定义是指数组里任一元素出现频数的最大值。
你的任务是在 nums 中找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。
分析:
给定一个数组,需要找到里面度最大的元素,然后求出这些元素中能够构成最短连续子数组的大小,情况如下:
【1,2,3,3,1】度最大=2,且包括元素1,3,但是最短连续子数组是3,其长度为2
【1,2,1,3,1】度最大=3,为元素1,最短子数组长度为3.
思路:利用一个哈希表,记录元素,然后找到里面最大的度以及度对应的元素有哪些,然后分别对这些元素进行遍历,求出它们的最短子数组(但是在力扣中出错了,因为数组大小越界被限制),但是思路没问题。
解析:
1.暴力法(有问题)
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int>map;
vector<int>vec;
int res = 0;
int ans = 0;
int q = 0;
int w = 0;
int e = 0;
int r = 100000;
for (auto num : nums)
{
map[num]++;
res = max(map[num], res);//找到了度的大小
}
e = res;
for (std::unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++)
{
if (it->second == res)
{
ans = it->first;
vec.push_back(ans);//插入度最大的元素,可能有多个
}
}
for (int j=0;j<vec.size();j++)
{
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] == vec[j] && e == res)
{
q = i;
e--;
}
else if (nums[i] == vec[j] && e == 1)
{
w = i;
}
else if (nums[i] == vec[j] && e != res && e != 1)
{
e--;
}
else if (e == 0)
{
r = q+1;
return r;
}
}
e = res;
r = min(r, w - q + 1);
}
return r;
}
};
2.优化
在求度以及对应元素时,利用一个vector容器一次型存放,这样方便后面的计算
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, vector<int>> mp;
int n = nums.size();
for (int i = 0; i < n; i++) {
if (mp.count(nums[i])) {
mp[nums[i]][0]++;
mp[nums[i]][2] = i;
}
else {
mp[nums[i]] = { 1, i, i };
}
}
int maxNum = 0, minLen = 0;
for (auto& [_, vec] : mp) {
if (maxNum < vec[0]) {
maxNum = vec[0];
minLen = vec[2] - vec[1] + 1;
}
else if (maxNum == vec[0]) {
if (minLen > vec[2] - vec[1] + 1) {
minLen = vec[2] - vec[1] + 1;
}
}
}
return minLen;
}
};
边栏推荐
- 晶振工作原理详解
- [Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails
- The untiy Resources directory dynamically loads resources
- DataTable Helper Class for C#
- hcip第九天
- 银行案例|Zabbix跨版本升级指南,4.2-6.0不香吗?
- 金仓数据库KingbaseES安全指南--6.9. Ident身份验证
- 06 redis 集群搭建
- 11 一发布就发布一系列系列
- 泰国 好产品推荐!2022年最好的胶原蛋白评测有哪些? 喝出健康和美丽适合需要改善肌肤
猜你喜欢
[Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails
Ali's official Redis development specification
ROS2系列知识(5):【参数】如何管理?
Unity ui点击事件只响应最上层ui的方式
缓存一致性MESI与内存屏障
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
统信软件、龙芯中科等四家企业共同发布《数字办公安全创新方案》
沈腾拯救暑期档
MySQL INTERVAL Keyword Guidelines
The anxiety of the post-90s was cured by the vegetable market
随机推荐
Winform message prompt box helper class
金仓数据库 OCCI迁移指南(2. 概述)
不需要写代码,快速批量修改文件夹中图片的格式
ROS2系列知识(6):Action服务概念
工业制造行业的低代码开发平台思维架构图
Are online account opening commissions reliable? Is online account opening safe?
DateTime Helper Class for C#
Rancher 部署 DataKit 最佳实践
The anxiety of the post-90s was cured by the vegetable market
Unity ui点击事件只响应最上层ui的方式
直播app开发,是优化直播体验不得不关注的两大指标
比对软件-blastN结果详解
Sftp中文件名乱码
MySQL's maximum recommended number of rows is 2000w, is it reliable?
Vulnhub target drone: HARRYPOTTER_ NAGINI
酷逼了 Pathetic Dog 第 304 场周赛
个人日记
70后夫妻给苹果华为做“雨衣”,三年进账7.91亿
C# CSV format file helper class
今晚直播!