两数组包含问题(来自微软面试题)
2013-04-08 23:24
225 查看
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)
请同时参考 /article/2783248.html
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)
请同时参考 /article/2783248.html
// 两数组包含问题(来自微软面试题).cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> using namespace std; void FindConsecutiveSubstringLocator(int A[], int lenA, int B[], int lenB, int &Pos1, int &Pos2) { std::map<int,int> mapB; std::map<int,int> mapWindow; for (int idx = 0; idx < lenB; idx++) { if (mapB.count(B[idx]) == 0) mapB[B[idx]] = 1; else mapB[B[idx]]++; } for (int i = 0; i <= lenA-lenB;) { int j = i; for (; j < i+lenB; j++) { if (mapB.count(A[j]) > 0) { if (mapWindow.count(A[j]) == 0) { mapWindow[A[j]] = 1; } else { if (mapWindow[A[j]] < mapB[A[j]]) { mapWindow[A[j]]++; } else { for (int k = i; k < j; k++) { if (A[k] == A[j]) { i = k+1; mapWindow.clear(); break; } } break; } } } else { i++; mapWindow.clear(); break; } } if (j == i+lenB) { Pos1 = j-lenB; Pos2 = j-1; return; } } } int _tmain(int argc, _TCHAR* argv[]) { int A[] = {4, 1, 2, 1, 8, 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); int pos1 = 0, pos2 = 0; FindConsecutiveSubstringLocator(A, lenA, B, lenB, pos1, pos2); return 0; }
相关文章推荐
- 面试题精选(87):两数组包含问题(来自微软面试题)
- 终于搞清楚了C#二进制的一些关键操作了,解决了微软面试题,求数组中两两之差绝对值最小的值O(N)最少内存限制的问题!
- 一道经典面试题,字符数组排序问题(字符包含a-z、A-Z、0-9.)
- 北美微软面试题---数组问题
- 微软面试题---求子数组最大乘积问题
- 【我解C语言面试题系列】004 数组的循环右移问题
- 【百度面试题】循环有序数组的查找问题
- 微软面试题 寻找数组中出现的唯一重复的一个数
- Java面试题:1-100之间的数,被放在数组a[99]中,有一个数没有包含在其中,用java代码找出这个数
- 微软面试题---阶乘问题
- 求子数组的最大和问题--一道浙江大学考研压轴题(被Google拿来做面试题)
- [分类整理IV]微软等100题系列V0.1版:字符串+数组面试题集锦
- 【我解C语言面试题系列】004 数组的循环右移问题
- 【我解C语言面试题系列】009 特殊的去除数组中重复数字问题
- 【面试题】k序数组的排序问题
- 【面试题】输出数组的问题
- 多个div并排显示的居中问题——来自腾讯的一道面试题
- Java面试题:1-100之间的数,被放在数组a[99]中,有一个数没有包含在其中,用java代码找出这个数
- 微软面试题----钢管取球问题
- 微软面试题:求两升序排序数组两两数字之和中,最小的k个值