[leetcode]599. Minimum Index Sum of Two Lists
Analysis
ummm~—— [ummmm~]
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
遍历两个array,然后找到相同的元素,求下标和,把下标和最小的相同元素输出(可能不止一个哦~)。
Implement
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
int len1 = list1.size();
int len2 = list2.size();
vector<string> res;
int min_sum = INT_MAX;
for(int i=0; i<len1; i++){
int sum;
for(int j=0; j<len2; j++){
if(list1[i] == list2[j]){
sum = i + j;
if(sum < min_sum){
res.clear();
res.push_back(list1[i]);
min_sum = sum;
}
else if(sum == min_sum)
res.push_back(list1[i]);
break;
}
}
}
return res;
}
};