[抄题]:

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.

Example 1:

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".

Example 2:

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).

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

两次求和的长度有可能相等,都是最大,需要直接往结果里添加

[思维问题]:

以为要存两个hashset。但其实直接取,判断是否为空即可,能少存一次

[一句话思路]:

存一次,取一次

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 有新的最小值之和时,记得实时更新

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

存一次,取一次

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

res.toArray(new String[res.size()]) list变成数组,里面再放字符串,字符串还要指定大小。转了3次。

直接指定string的尺寸:用方括号

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
//ini: hashmap, linkedlist
Map<String, Integer> map = new HashMap<>();
List<String> res = new LinkedList<>();
int minSum = Integer.MAX_VALUE; //put list1 into hashmap
for (int i = 0; i < list1.length; i++) {
map.put(list1[i], i);
} //get from list2
for (int i = 0; i < list2.length; i++) {
Integer j = map.get(list2[i]);
if (j != null && i + j <= minSum) {
if (i + j < minSum) {
res.clear();
minSum = i + j;
}
res.add(list2[i]);
}
} //return, change form
return res.toArray(new String[res.size()]);
}
}

599. Minimum Index Sum of Two Lists两个餐厅列表的索引和最小的更多相关文章

  1. 【Leetcode_easy】599. Minimum Index Sum of Two Lists

    problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...

  2. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...

  3. LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  4. 599. Minimum Index Sum of Two Lists

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  5. [LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  6. 599. Minimum Index Sum of Two Lists(easy)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  7. [LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  8. LC 599. Minimum Index Sum of Two Lists

    题目描述 Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fav ...

  9. LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists

    题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...

随机推荐

  1. struts2逻辑视图类型汇总与解释(转)

    在struts2框架中,当action处理完之后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身. 结果类型提供了返回给用户信息类型的实现细节.结果类型通常在Struts2中就已预定 ...

  2. 在zxing开源项目里,camera.setDisplayOrientation(90)出现错误

    [错误提示]  setDisplayOrientation(int)未定义 [错误原因]  sdk版本过低,这个方法在Android2.2之后才有 [解决方法]  直接在project.propert ...

  3. Python之os.path

    os.path模块主要用于常用路径名操作,在编程中使用频率特高 os.path常用函数 abspath(path) 返回路径名path的规范化的绝对路径 split(path) 将path分割成目录和 ...

  4. matlab中hdl coder 的使用

    今天摸索了一下hdl coder的使用方法,各个步骤主要是照猫画虎,有些地方还是不理解,先总结一下: 1.要想调用quartus或者Xilinx综合布局布线需要先设置,设置的方法有两种,命令窗口输入 ...

  5. dockerfile初试之tomcat8封装

    前面学习了一些docker相关资料,有看到dockerfile这个东东,一时没看得太明白,理论给合实践是最好的学习方法,自己做一下就行了嘛.主要步聚记录如下: 0)环境 10.202.105.96   ...

  6. 记一次RESTful调试过程

    1. 为什么前台怎么调用后台,都是跳到页面不存在. 因为已经改为RESTful,保存按钮的type还是submit: 2. 改成buttong之后,设置onclick="update()“, ...

  7. Jmeter ----关于上传图片接口

    转自:http://www.cnblogs.com/linglingyuese/p/4514808.html 需求 1 2 3 4 5 6 7 8 9 post上传   Request: { &quo ...

  8. ISO模型的七个分层

    要想理解socket首先得熟悉一下TCP/IP协议族, TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,定义了主 ...

  9. Effective java笔记3--类和接口1

    一.使类和成员的可访问能力最小化 要想区别一个设计良好的模块与一个设计不好的模块,最重要的因素是,这个模块对于外部的其他模块而言,是否隐藏了内部的数据和其他的实现细节.一个设计良好的模块会隐藏所有的实 ...

  10. 1100 Mars Numbers

    题意:进制转换. 思路:注意当数字是13的倍数时,只需高位叫法的单词.比如26,是“hel”,而不是“hel tret”.我被坑在这里了!对应语句1的处理.另外,在输入n和n个字符串之间需要一个吸收字 ...