当前位置:网站首页>力扣每日一题-第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;
}
};
边栏推荐
- 第一次改开源中间件keycloak总个结
- 指针和解引用
- The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
- 星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
- DataTable Helper Class for C#
- 08 spark 集群搭建
- Vulnhub靶机:HARRYPOTTER_ NAGINI
- zabbix部署和简单使用
- 开发工具:第五章:使用idea生成实体类
- 05 Doris cluster construction
猜你喜欢

Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?

【Unity,C#】哨兵点位循迹模板代码

参观首钢园

The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!

JumpServer堡垒机部署

ROS2系列知识(7):用rqt_console查看日志logs

The anxiety of the post-90s was cured by the vegetable market

08 Spark cluster construction

Vulnhub靶机:HARRYPOTTER_ NAGINI

阿里官方 Redis 开发规范
随机推荐
关于LocalDateTime的全局返回时间带“T“的时间格式处理
C#Excel帮助类
Bugku-Misc-贝斯手
网上开户佣金万一靠谱吗,网上开户安全吗
5年测试,只会功能要求17K,功能测试都敢要求这么高薪资了?
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(2. 概述)
网站备案后没有找到站点 您没有将此域名或IP绑定到对应站点! 配置文件未生效!
参观首钢园
MySQL INTERVAL Keyword Guidelines
ROS2系列知识(5):【参数】如何管理?
03 gp cluster construction
指针和解引用
完全背包问题求组合数和排列数
请问数据库中报错信息如下,mongoshake 有什么配置的方式解决这种大消息问题吗?
UI helper class for Winform - some components will use DevExpress components
Winform的UI帮助类——部分组件会使用到DevExpress组件
LeetCode第 303 场周赛
MySQL最大建议行数2000w, 靠谱吗?
RecSys'22|CARCA:交叉注意力感知上下文和属性进行推荐
Flask框架实战