You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.2 KiB

2 years ago
/*/*******************************************************************************
** **
** Jiedi(China nanjing)Ltd. **
** **
*******************************************************************************/
/*****************************FILE INFOMATION***********************************
**
** Project : ---leetcode
** Contact : xiacaojun@qq.com
** : http://blog.csdn.net/jiedichina
** : http://study.163.com/u/xiacaojun
https://jiedi.ke.qq.com/
csdn https://edu.csdn.net/course/detail/25037
** 51cto http://edu.51cto.com/lecturer/index/user_id-100013755.html
** http://www.laoxiaketang.com
**
** ---leetcode 296249312
** : jiedi2007
** :
**
*****************************************************************************
// 算法设计与编程实践---基于leetcode的企业真题库 课程 QQ群296249312 下载代码和交流*/
/*
* @lc app=leetcode.cn id=697 lang=cpp
*
* [697]
*/
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
int n = nums.size();
map<int,vector<int>> tmp;//vector记录出现的相同数的位置
for(int i=0;i<n;i++){
tmp[nums[i]].push_back(i);
}
int count = 0; //表征数组的度
int res = INT_MAX;
for(auto iter=tmp.begin();iter!=tmp.end();iter++){
int m = iter->second.size();//算一下数组的度
if(m>count){
count = m;
res = iter->second[m-1]-iter->second[0]+1;
}
if(m==count){
res=min(res,iter->second[m-1]-iter->second[0]+1);
}
}
return res;
}
};