# 中兴编程题－－获取未命中个数

max_cache_size: 2
page_requests:[1, 2, 1, 3, 1, 2]

5。

# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

int getMissNum(const vector<int> &nums, const unsigned int &n)
{
if (n <= 0 || nums.empty())
return 0;
vector<int> Q;
int cnt = 0;
for (unsigned int i = 0; i < nums.size(); ++i)
{
if (Q.empty())
{
Q.insert(Q.begin(), nums[i]);
cnt ++;
}
else
{
if(find(Q.begin(), Q.end(), nums[i]) == Q.end())
{
cnt ++;
if(Q.size() == n)
{
Q.pop_back();
Q.insert(Q.begin(), nums[i]);
}
else
{
Q.insert(Q.begin(), nums[i]);
}
}
}
}
return cnt;
}

int main(void)
{
int nums_temp1[] = {1, 2, 1, 3, 1, 2};
int nums_temp2[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0};
int nums_temp3[] = {2, 3, 1, 3, 2, 1, 4, 3, 2};
vector<int> nums1(nums_temp1, nums_temp1 + 6);
vector<int> nums2(nums_temp2, nums_temp2 + 16);
vector<int> nums3(nums_temp3, nums_temp3 + 9);
cout << "case1, missNum is " << getMissNum(nums1, 2) << endl;
cout << "case2, missNum is " << getMissNum(nums2, 3) << endl;
cout << "case3, missNum is " << getMissNum(nums3, 2) << endl;
return 0;
}

vector模拟队列是用insert()和pop_back()两个函数实现的，即压入元素insert(Q.begin(), nums[i])，弹出元素pop_back()。

• 本文已收录于以下专栏：

举报原因： 您举报文章：中兴编程题－－获取未命中个数 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)