Minimum Index Sum of Two Lists问题及解法
来源:互联网 发布:酒店2000w数据 编辑:程序博客网 时间:2024/06/03 13:11
问题描述:
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.
示例:
Input:["Shogun", "Tapioca Express", "Burger King", "KFC"]["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]Output: ["Shogun"]Explanation: The only restaurant they both like is "Shogun".
Input:["Shogun", "Tapioca Express", "Burger King", "KFC"]["KFC", "Shogun", "Burger King"]Output: ["Shogun"]Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).问题分析:
遍历两个数组,统计相同元素的索引和,始终将索引和最小的保存下来。最后,根据索引求元素值。
过程详见代码:
class Solution {public: vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) { vector<vector<pair<int, int>>> res(1),temp(1);int mini = list1.size() + list2.size();for (int i = 0; i < list1.size(); i++){for (int j = 0; j < list2.size(); j++){if (list1[i] == list2[j]){if (i + j < mini){mini = i + j;temp = res;temp[0].push_back(pair<int, int>(i, j));}else if (i + j == mini){temp[0].push_back(pair<int, int>(i, j));}}}}vector<string> vres;for (int i = 0; i < temp[0].size(); i++){vres.push_back(list1[temp[0][i].first]);}return vres; }};
阅读全文
0 0
- Minimum Index Sum of Two Lists问题及解法
- 599. Minimum Index Sum of Two Lists的C++解法
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- 599. Minimum Index Sum of Two Lists
- leetcode[Minimum Index Sum of Two Lists]//待整理多种解法
- [leetcode] 599. Minimum Index Sum of Two Lists
- leetcode 599. Minimum Index Sum of Two Lists
- leetode.599. Minimum Index Sum of Two Lists
- springboot利用swagger构建api文档
- BZOJ 3052: [wc2013]糖果公园 树上莫队
- js开源插件速查表
- git新建本地仓库并上传到云端
- windows 系统常用 dll
- Minimum Index Sum of Two Lists问题及解法
- linux 学习20170726 查看进程号及环境变量、获取字符串长度及shell版本
- 2017年多校赛第一场 1006 Function(枚举)
- 完全分布式安装Hadoop,Hive,Hbase,Hwi,Zookeeper
- Python assert断言
- caffe 编译/usr/local/lib/libopencv_highgui.so: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'
- Spark SQL连接MySQL示例
- 模式: 每个容器的一个服务实例
- 《软件工程专业基础综合》考试大纲