leetcode面试题17.11单词距离
作者:互联网
思路:
双指针。
实现:
1 class Solution { 2 public: 3 int findClosest(vector<string>& words, string word1, string word2) { 4 int n=words.size(); 5 int res=100001; 6 int id1=-1,id2=-1; 7 for(int i=0;i<n;i++){ 8 if(words[i]==word1){ 9 id1=i; 10 } 11 else if(words[i]==word2){ 12 id2=i; 13 } 14 if(id1>=0 and id2>=0){ 15 res=min(res,abs(id2-id1)); 16 } 17 } 18 return res; 19 } 20 };
标签:面试题,string,int,res,id2,id1,17.11,words,leetcode 来源: https://www.cnblogs.com/wangyiming/p/16324289.html