[LeetCode]599.Minimum Index Sum of Two List
来源:互联网 发布:中国导弹技术 知乎 编辑:程序博客网 时间:2024/06/06 03:47
题目大意:点击打开链接
思路:找intersection的部分,思路来自于Intersection of Two Arrays:点击打开链接
public class Solution { public String[] findRestaurant(String[] list1, String[] list2) { Map<String,Integer> map=new HashMap<>(); for(int i=0;i<list1.length;i++){ map.put(list1[i],i); } List<String> list=new ArrayList<>(); int indexSum=0; int minSum=Integer.MAX_VALUE; for(int i=0;i<list2.length;i++){ if(map.containsKey(list2[i])){ indexSum=map.get(list2[i])+i; if(indexSum<minSum){ minSum=indexSum; list.clear(); //每次有更小的minSum,更新minSum的时候,都要清空list list.add(list2[i]); }else if(indexSum==minSum){ //相同的minSum直接添加 list.add(list2[i]); } } } String[] result=new String[list.size()]; for(int i=0;i<result.length;i++){ result[i]=list.get(i); } return result; //或者return list.toArray(new String[list.size()]); }}
阅读全文
0 0
- [LeetCode]599.Minimum Index Sum of Two List
- [leetcode] 599. Minimum Index Sum of Two Lists
- leetcode 599. Minimum Index Sum of Two Lists
- LeetCode 599. Minimum Index Sum of Two Lists(map,unordered_map)
- 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]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
- 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
- 【优雅编程之道】之枚举的6点建议
- 手把手搭建一个完整的javaweb项目(适合新手)
- (项目)显示调用controller后显示404页面
- 石像鬼 openwrt chinadns dns-forwarder 开机无法自动启动
- (项目)c标签表达式<c:foreach varStatus="i">的使用
- [LeetCode]599.Minimum Index Sum of Two List
- 修改maven的默认jdk版本为本地安装的版本
- http 文件上传和下载协议分析(包括多段下载)
- (项目)在浏览器控制台报400的错误,怎么处理
- keras安装
- (项目)首页就报500的错,首页显示不了。
- (项目)页面显示数据为null
- (dao)Connection is read-only. Queries leading to data modification are not allowed
- 作为Java面试官--谈谈一年来的面试总结