面试题 17.11
Solutions
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int index1 = -0x3f3f3f3f, index2 = -0x3f3f3f3f;
int res = INT_MAX;
for (int i = 0; i < words.size(); i++) {
if (words[i] == word1) {
index1 = i;
res = min(res, index1 - index2);
}
else if (words[i] == word2) {
index2 = i;
res = min(res, index2 - index1);
}
}
return res;
}
};Last updated