面试题精选(87):两数组包含问题(来自微软面试题)
2010-03-26 22:01
676 查看
题目:
You have given two arrays, say
A: 4, 1, 6, 2, 8, 9, 5, 3, 2, 9, 8, 4, 6
B: 6, 1, 2, 9, 8
where B contains elements which are in A in consecutive locations but may be in any order.
Find their starting and ending indexes in A. (Be careful of duplicate numbers).
answer is (1,5)
先给出代码,再结合代码解释,如下:
思路:
ex,
int A[] = {4, 1, 2, 1, 8, 9, 5, 3, 2, 9, 8, 4, 6};
int B[] = {1, 1, 2, 8, 9};
int lenA = 13;
int lenB = 5;
map<int,int> bmap;
map<int,int> windowmap;
map<int,int> diffmap;
首先初始化map:
bmap = {1:2, 2:1, 8:1,9:1}
windowmap = {1:2, 2:1, 4:1,8:1}
diffmap = {1:0, 2:0, 8:0, 9:-1}
其中"1: 0" 意味着数组A的当前滑动窗口正好和数组B包含相同数量的"1",而 " 9:-1" 则表示A当前的滑动窗口和B比较缺少1个"9"。 并且我们注意到diffmap和bmap拥有相同的key
代码中的变量"sameElement"代表的是diffmap中有多少pair与bmap匹配, 显然,初始化后的“sameElement”变量值会是3 ( 1:0, 2:0, 8:0 in diffmap).
接下来
在数组A中滑动size为lenB的窗口,没向前滑动一步,只需check滑动窗口左侧划出的元素El和右侧滑入的元素Er,更新diffmap和sameElement,如下:
if(diffmap.count(A[i-lenB]) != 0)
{
diffmap[A[i-lenB]]--;
if(diffmap[A[i-lenB]] == 0)
sameElement++;
else if(diffmap[A[i-lenB]] == -1)
sameElement--;
}
if(diffmap.count(A[i]) !=0)
{
diffmap[A[i]]++;
if(diffmap[A[i]] == 0)
sameElement++;
else if(diffmap[A[i]] == 1)
sameElement--;
}
if(sameElement == diffmap.size())
{
cout<<"----------find one---------"<<endl;
cout<<"start index:"<<i-lenB+1<<endl;
cout<<"end index:"<<i+1<<endl;
}
You have given two arrays, say
A: 4, 1, 6, 2, 8, 9, 5, 3, 2, 9, 8, 4, 6
B: 6, 1, 2, 9, 8
where B contains elements which are in A in consecutive locations but may be in any order.
Find their starting and ending indexes in A. (Be careful of duplicate numbers).
answer is (1,5)
先给出代码,再结合代码解释,如下:
#include <iostream> #include <map> using namespace std; void FindConsecutiveSubarrLocation(int A[], int lenA, int B[], int lenB) { map<int, int> bmap; map<int, int> windowmap; map<int, int> diffmap; for(int i = 0; i< lenB; i++) { if(bmap.count(B[i]) == 0) bmap[B[i]] = 1; else ++bmap[B[i]]; if(windowmap.count(A[i]) == 0) windowmap[A[i]] = 1; else ++windowmap[A[i]]; } map<int, int>::iterator it = bmap.begin(); int sameElement = 0; while(it != bmap.end()) { if(windowmap.count((*it).first) != 0) { diffmap[(*it).first] = windowmap[(*it).first] - (*it).second; if(diffmap[(*it).first] == 0) sameElement++; } else diffmap[(*it).first] = -(*it).second;; it++; } if(sameElement == lenB) { cout<<"----------find one---------"<<endl; cout<<"start index:"<<0<<endl; cout<<"end index:"<<lenB-1<<endl; } for(int i = lenB; i < lenA; i++) { if(diffmap.count(A[i-lenB]) != 0) { diffmap[A[i-lenB]]--; if(diffmap[A[i-lenB]] == 0) sameElement++; else if(diffmap[A[i-lenB]] == -1) sameElement--; } if(diffmap.count(A[i]) !=0) { diffmap[A[i]]++; if(diffmap[A[i]] == 0) sameElement++; else if(diffmap[A[i]] == 1) sameElement--; } if(sameElement == diffmap.size()) { cout<<"----------find one---------"<<endl; cout<<"start index:"<<i-lenB+1<<endl; cout<<"end index:"<<i+1<<endl; } } } int main() { int A[] = {4, 1, 2, 1, 8, 9, 2, 1, 2, 9, 8, 4, 6}; int B[] = {1, 1, 2, 8, 9}; int lenA = sizeof(A)/sizeof(int); int lenB = sizeof(B)/sizeof(int); FindConsecutiveSubarrLocation(A, lenA, B, lenB); return 0; }
思路:
ex,
int A[] = {4, 1, 2, 1, 8, 9, 5, 3, 2, 9, 8, 4, 6};
int B[] = {1, 1, 2, 8, 9};
int lenA = 13;
int lenB = 5;
map<int,int> bmap;
map<int,int> windowmap;
map<int,int> diffmap;
首先初始化map:
bmap = {1:2, 2:1, 8:1,9:1}
windowmap = {1:2, 2:1, 4:1,8:1}
diffmap = {1:0, 2:0, 8:0, 9:-1}
其中"1: 0" 意味着数组A的当前滑动窗口正好和数组B包含相同数量的"1",而 " 9:-1" 则表示A当前的滑动窗口和B比较缺少1个"9"。 并且我们注意到diffmap和bmap拥有相同的key
代码中的变量"sameElement"代表的是diffmap中有多少pair与bmap匹配, 显然,初始化后的“sameElement”变量值会是3 ( 1:0, 2:0, 8:0 in diffmap).
接下来
在数组A中滑动size为lenB的窗口,没向前滑动一步,只需check滑动窗口左侧划出的元素El和右侧滑入的元素Er,更新diffmap和sameElement,如下:
if(diffmap.count(A[i-lenB]) != 0)
{
diffmap[A[i-lenB]]--;
if(diffmap[A[i-lenB]] == 0)
sameElement++;
else if(diffmap[A[i-lenB]] == -1)
sameElement--;
}
if(diffmap.count(A[i]) !=0)
{
diffmap[A[i]]++;
if(diffmap[A[i]] == 0)
sameElement++;
else if(diffmap[A[i]] == 1)
sameElement--;
}
if(sameElement == diffmap.size())
{
cout<<"----------find one---------"<<endl;
cout<<"start index:"<<i-lenB+1<<endl;
cout<<"end index:"<<i+1<<endl;
}
相关文章推荐
- 两数组包含问题(来自微软面试题)
- 一道经典面试题,字符数组排序问题(字符包含a-z、A-Z、0-9.)
- 微软面试题---求子数组最大乘积问题
- 面试题精选(81):两道按照要求重排数组问题
- 终于搞清楚了C#二进制的一些关键操作了,解决了微软面试题,求数组中两两之差绝对值最小的值O(N)最少内存限制的问题!
- 北美微软面试题---数组问题
- Java面试题:1-100之间的数,被放在数组a[99]中,有一个数没有包含在其中,用java代码找出这个数
- 程序员面试题精选100题(47)-数组中出现次数超过一半的数字
- 算法--微软面试题:求一个整数数组元素间最小差值
- 最广为流传的面试问题-微软面试题
- 127名选手1:1淘汰赛决出优胜者问题 - Google, 谷歌,百度,baidu,阿里巴巴,alibaba,微软,华为,huawei面试题
- 【面试题三】c数组做为参数退化的问题,二维数组中的查找
- 【我解C语言面试题系列】008 去除数组中重复数字问题
- 面试题精选(75):经过旋转的有序数组中的元素查找(要求复杂度为O(lgn))
- 5. 微软面试题: 查找最小的k个元素(数组)
- [程序员面试题精选100题]58.八皇后问题
- [面试题精选]微软试题
- [面试题精选]微软试题
- 精选11道Java技术面试题并有答案(包含部分阿里和华为的面试题)
- 精选11道Java技术面试题并有答案(包含部分阿里和华为的面试题)