599:两个列表的最小索引总和

This commit is contained in:
huangge1199 2021-07-09 16:30:17 +08:00
parent d147e61698
commit a7fe7f8436
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,72 @@
//假设Andy和Doris想在晚餐时选择一家餐厅并且他们都有一个表示最喜爱餐厅的列表每个餐厅的名字用字符串表示
//
// 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅 如果答案不止一个则输出所有答案并且不考虑顺序 你可以假设总是存在一个答案
//
// 示例 1:
//
// 输入:
//["Shogun", "Tapioca Express", "Burger King", "KFC"]
//["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
//输出: ["Shogun"]
//解释: 他们唯一共同喜爱的餐厅是Shogun
//
//
// 示例 2:
//
// 输入:
//["Shogun", "Tapioca Express", "Burger King", "KFC"]
//["KFC", "Shogun", "Burger King"]
//输出: ["Shogun"]
//解释: 他们共同喜爱且具有最小索引和的餐厅是Shogun它有最小的索引和1(0+1)
//
//
// 提示:
//
//
// 两个列表的长度范围都在 [1, 1000]
// 两个列表中的字符串的长度将在[130]的范围内
// 下标从0开始到列表的长度减1
// 两个列表都没有重复的元素
//
// Related Topics 数组 哈希表 字符串
// 👍 112 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
//599:两个列表的最小索引总和
public class MinimumIndexSumOfTwoLists{
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumIndexSumOfTwoLists().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
HashMap< Integer, List < String >> map = new HashMap < > ();
for (int i = 0; i < list1.length; i++) {
for (int j = 0; j < list2.length; j++) {
if (list1[i].equals(list2[j])) {
if (!map.containsKey(i + j)) {
map.put(i + j, new ArrayList < String > ());
}
map.get(i + j).add(list1[i]);
}
}
}
int min_index_sum = Integer.MAX_VALUE;
for (int key: map.keySet()) {
min_index_sum = Math.min(min_index_sum, key);
}
String[] res = new String[map.get(min_index_sum).size()];
return map.get(min_index_sum).toArray(res);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,31 @@
<p>假设Andy和Doris想在晚餐时选择一家餐厅并且他们都有一个表示最喜爱餐厅的列表每个餐厅的名字用字符串表示。</p>
<p>你需要帮助他们用<strong>最少的索引和</strong>找出他们<strong>共同喜爱的餐厅</strong>。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>
[&quot;Shogun&quot;, &quot;Tapioca Express&quot;, &quot;Burger King&quot;, &quot;KFC&quot;]
[&quot;Piatti&quot;, &quot;The Grill at Torrey Pines&quot;, &quot;Hungry Hunter Steakhouse&quot;, &quot;Shogun&quot;]
<strong>输出:</strong> [&quot;Shogun&quot;]
<strong>解释:</strong> 他们唯一共同喜爱的餐厅是&ldquo;Shogun&rdquo;
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>
[&quot;Shogun&quot;, &quot;Tapioca Express&quot;, &quot;Burger King&quot;, &quot;KFC&quot;]
[&quot;KFC&quot;, &quot;Shogun&quot;, &quot;Burger King&quot;]
<strong>输出:</strong> [&quot;Shogun&quot;]
<strong>解释:</strong> 他们共同喜爱且具有最小索引和的餐厅是&ldquo;Shogun&rdquo;它有最小的索引和1(0+1)。
</pre>
<p><strong>提示:</strong></p>
<ol>
<li>两个列表的长度范围都在&nbsp;[1, 1000]内。</li>
<li>两个列表中的字符串的长度将在[130]的范围内。</li>
<li>下标从0开始到列表的长度减1。</li>
<li>两个列表都没有重复的元素。</li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>字符串</li></div></div>\n<div><li>👍 112</li><li>👎 0</li></div>