class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2)
{
int res = INT_MAX;
int index1 = INT_MIN;
int index2 = INT_MIN;
for (int i = 0; i < words.size(); i++)
{
if (words[i] == word1)
{
index1 = i;
}
if (words[i] == word2)
{
index2 = i;
}
if (index1 != INT_MIN && index2 != INT_MIN) res = min(res, abs(index1 - index2));
if (res == 1) break;
}
return res;
}
};
leetcode 面试题 17.11. 单词距离
最新推荐文章于 2024-07-09 23:43:07 发布