首页 > 其他 > 详细

【字符串】面试题 17.11. 单词距离

时间:2020-05-04 16:17:34      阅读:71      评论:0      收藏:0      [点我收藏+]

题目:

技术分享图片

 

 

 

解答:

 1 class Solution {
 2 public:
 3     int findClosest(vector<string>& words, string word1, string word2) 
 4     {
 5 
 6         int t1 = -1;
 7         int t2 = -1;
 8         int res = words.size();
 9 
10         for (int i = 0; i < words.size(); i ++) 
11         {
12             if (words[i] == word1) 
13             {
14                 t1 = i;
15             }
16             else if (words[i] == word2) 
17             {
18                 t2 = i;
19             }
20             if (t1 != -1 && t2 != -1) 
21             {
22                 res = min(res, abs(t1 - t2));
23             }
24             if (res == 1) 
25             {
26                 break;
27             }
28         }
29         return res;
30     }
31 };

 

【字符串】面试题 17.11. 单词距离

原文:https://www.cnblogs.com/ocpc/p/12826614.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!