599. Minimum Index Sum of Two Lists

Posted 高数考了59

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了599. Minimum Index Sum of Two Lists相关的知识,希望对你有一定的参考价值。

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) 
12     {
13         unordered_map<string,int> simap;
14         int sz1=list1.size();
15         int sz2=list2.size();
16         for(int i=0;i<sz1;i++)
17             simap[list1[i]]=i;
18         int count=INT_MAX;
19         vector<string> res;
20         for(int i=0;i<sz2;i++)
21         {
22             if(simap.find(list2[i])!=simap.end())
23             {
24                 int cur=i+simap[list2[i]];
25                 if(cur==count)
26                     res.push_back(list2[i]);
27                 else if(cur<count)
28                 {
29                     res.clear();
30                     res.push_back(list2[i]);
31                     count=cur;
32                 }
33             }
34         }
35         return res;
36     }
37 };

用个map,挺简单,问题不大

以上是关于599. Minimum Index Sum of Two Lists的主要内容,如果未能解决你的问题,请参考以下文章

599. Minimum Index Sum of Two Lists

599. Minimum Index Sum of Two Lists

[LeetCode] 599. Minimum Index Sum of Two Lists

[leetcode-599-Minimum Index Sum of Two Lists]

LeetCode 599. Minimum Index Sum of Two Lists

Leetcode_easy599. Minimum Index Sum of Two Lists