leetcode 面试题 17.11 单词距离
作者:互联网
直接记录出现二者的位置后比较取最小值即可
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int ans=1000000;int l=-1,r=-1;
for(int i=0;i<words.size();i++)
{
if(words[i]==word1)l=i;
if(words[i]==word2)r=i;
if(l!=-1&&r!=-1)
ans=min(ans,abs(l-r));
}
return ans;
}
};
标签:面试题,string,int,word2,17.11,word1,ans,leetcode 来源: https://www.cnblogs.com/lynko/p/16317960.html