LeetCode 599. Minimum Index Sum of Two Lists

FreeCAD MCP实战:用自然语言驱动AI建模,效率提升10倍 MCP(Model Context Protocol)是一种让AI模型调用外部工具和数据的协议标准,通过标准化通信层解决AI与CAD软件之间的交互难题。其原理是向AI暴露工具清单,AI根据自然语言需求自动选择工具并填充参数。在工程实践中,这一技术显著降低了CAD建模门槛,将重复性操作从手动点击转为语音指令,适用于批量零件生成、参数化设计等场景。FreeCAD作为开源CAD,凭借开放的Python API和脚本化能力,成为落地MCP的理想平台。本文基于FreeCAD MCP的实操经验,分享环境搭建、自然语言建 阅读详情

599. Minimum Index Sum of Two Lists

Description

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

Note:
1.The length of both lists will be in the range of [1, 1000].
2.The length of strings in both lists will be in the range of [1, 30].
3.The index is starting from 0 to the list length minus 1.
4.No duplicates in both lists.

Solution

  • 题意即给出两个字符串数组,让你判断两组字符串中对应相同的字符串的下标和最小的那个字符串。如有多组,不计顺序返回。
  • 我的想法很暴力,两层循环,第一层循环控制第一个数组,去找第二个数组中与它相同的字符串,同时记录下标和,如果需要更新,那么更新。 代码如下:
class Solution {
public:
    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
        int len1 = list1.size(),len2 = list2.size();
        vector<string> rnt;
        int minIndexSum = 2000;
        for (int i = 0;i < len1;i++) {
            bool flag = true;
            for (int j = 0;j < len2 && flag;j++) {
                if (list1[i] == list2[j]) {
                    if (i + j < minIndexSum) {
                        minIndexSum = i + j;
                        rnt.clear();
                        rnt.push_back(list1[i]);
                    }
                    else if (i + j == minIndexSum) {
                        rnt.push_back(list1[i]);
                    }
                    flag = false;
                }
            }
        }
        return rnt;
    }
};
  • 还有的优化方案是 将第二层(或者第一层)循环的线性查找替换为使用哈希表存储第二个字符串数组(或者第一个),从而将线性时间优化为O(1),在题解中拿来的代码如下,侵删。
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
    vector<string>res;
    unordered_map<string,int>m;
    int min=INT_MAX;
    for(int i=0;i<list1.size();i++) m[list1[i]]=i;
    for(int i=0;i<list2.size();i++)
        if(m.count(list2[i])!=0)
            if(m[list2[i]]+i<min) min=m[list2[i]]+i,res.clear(),res.push_back(list2[i]);
            else if(m[list2[i]]+i==min) res.push_back(list2[i]);
    return res;
}
  • 这也提供了一种对于特定类型的优化方案,今后如果遇到仅涉及查找类的操作,我们可以使用哈希表从而将线性时间优化至常数时间。
全志平台camera驱动开发(4)NVP6134芯片4路模拟视频BT656输入 前言 1. 调试平台: 全志V5/T7等有BT656或BT1120的并行接口,理论有DVP接口的一般都支持 2. NVP6134可使用多路视频同时输入 硬件连接 模拟camera1 模拟camera2 NVP6134 ------>NVCSI0 ... 阅读详情

相关推荐

【3D目标检测】MMdetection3d——搭建IS-Fusion环境+nuScenes数据集训练

搭建IS-Fusion环境+nuScenes数据集训练

qq_44703886的博客 2333

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

【代码】LeetCode 599. 两个列表的最小索引总和。

正在学算法的小朋友的博客 161

从“盲控”到“预见”:聊聊永磁同步电机负载观测器(PMSM Load Observer)如何让控制更丝滑

本文深入探讨了永磁同步电机负载观测器(PMSM Load Observer)如何通过前馈补偿和转动惯量辨识技术实现智能抗扰控制。从传统‘盲控’到预见性调节,负载观测器显著减少转速波动和能耗,提升系统响应速度。文章详细解析了观测器算法架构、工程调参技巧及前沿发展趋势,为电机控制领域提供实用解决方案。

weixin_33737774的博客 414

LeetCode 每日一题 599.两个列表的最小索引和(简单)

LeetCode每日一题

JIANGhanha的博客 299

金蝶云星空API实战:从零到一,用Postman打通业务数据流

本文详细介绍了如何使用Postman调试金蝶云星空API,从创建第三方应用到实战调试技巧,帮助开发者快速打通业务数据流。文章重点讲解了API配置的关键细节、常见问题排查方法以及自动化测试方案,提升开发效率与准确性。

weixin_33712987的博客 531

用Docker-Compose三节点部署生产级Doris集群:从配置文件到故障转移

本文详细介绍了如何使用Docker-Compose部署一个生产级的三节点Apache Doris集群。内容涵盖从核心架构理解、配置文件编写到集群初始化验证的全过程,并重点讲解了如何实现高可用、数据持久化、健康检查以及故障转移等生产级运维关键点,为中小团队提供了一种在容器化便利性与系统稳定性间取得平衡的可靠部署方案。

kkk56的博客 866

LeetCode599. 两个列表的最小索引总和

假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。示例 1:输入: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The .

1号帅比的博客 1282

【Qt】解决 FTH: (11284): *** Fault tolerant heap shim applied to current process. (亲测有效)

计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers。容错堆垫片应用到当前的过程。这通常是由于以前的崩溃。win + r,输入regedit。

Fanjufei的博客 836

leetcode 599. 两个列表的最小索引总和 求解思路

先来看下题目: 599. 两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。 示例 1: 输入: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The Grill at Torrey Pines", "Hu

bug_null的博客 401

pdb文件删除杂原子 HETATM;保留或去除水分子

这样操作水分子也会跟着删除,如果需要保留水分子。

weixin_42357472的博客 2659

C#对接EtherNet/IP协议 完整版|罗克韦尔AB PLC通信+CIP核心概念全解析

你做罗克韦尔(AB) PLC工业上位机开发,是唯一的首选协议,没有之一!EtherNet/IP(Ethernet Industrial Protocol)是基于标准以太网的工业级通信协议,由罗克韦尔自动化主导开发,是ODVA(开放设备网络供应商协会)主推的工业以太网标准,也是目前全球应用最广泛的工业以太网协议之一。原生适配罗克韦尔AB PLC:Micro800、CompactLogix、ControlLogix、Micro850等全系AB PLC原生标配EtherNet/IP,无需额外配置,开箱即用;

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 913

Leetcode 599 Minimum Index Sum of Two Lists

Leetcode 599 Minimum Index Sum of Two Listsclass Solution { public: vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) { unordered_map<string,int> nameAndOrder;//li

自己的学习笔记 364

【ArcGIS Engine二次开发】入门基础(2):ArcGIS开发方式(VBA、DLL、Add-in、Engine)对比

文章目录2.1 使用VBA进行桌面软件开发2.1.1 VBA开发方式2.1.1 VBA代码的安全性2.2 使用DLL进行桌面软件开发2.2.1 DLL开发方式2.2.2 DLL功能的应用部署2.3 使用Add-in进行桌面软件开发2.4 使用ArcGIS Engine构建独立应用程序 对于桌面GIS应用来说,ArcGIS平台不仅提供了用于开发独立运行的GIS应用软件的二次开发工包ArcGIS Engine,还提供了对ArcGIS Deskop桌面软件进行扩展的开发的话,在开发个具体的GIS 行业应用软件时

智慧空间实验室 6523

[leetcode]599. Minimum Index Sum of Two Lists

[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 repre...

weixin_32135877的博客 121

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

leetcode 599. Minimum Index Sum of Two Lists

目录 一、问题描述 二、代码实现 1、暴力法 2、使用HashMap https://leetcode.com/problems/minimum-index-sum-of-two-lists/ 从两个人喜欢的餐厅列表中找到共同喜欢并下标之和最小的餐厅(如果有多个则将它们都返回) 一、问题描述 测试用例: Example 1: Input: ["Shogun", "T...

小景的博客 246

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【Leetcode】599. Minimum Index Sum of Two Lists

题目地址: https://leetcode.com/problems/minimum-index-sum-of-two-lists/ 给定两个字符串数组AAA和BBB,要求返回两个数组的交集中下标和最小的那些字符串。有可能有多个答案,所以要求返回数组。 不妨设AAA的长度小于等于BBB,接着用一个哈希表记录AAA中字符串及每个字符串下标,然后遍历BBB,若发现哈希表存了当前字符串,则判断下标和是否更小,若更小,则更新最小下标和,并清空答案,并把这个字符串加进去;若相等,则不用清空答案,直接加进去;若更大,

数学、算法爱好者的博客 192

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

【LeetCode】599.Minimum Index Sum of Two Lists(Easy)解题报告题目地址:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目描述:   Suppose Andy and Doris want to choose a restaurant for di

郝春雨的博客 256

LeetCode //C - 599. Minimum Index Sum of Two Lists

【代码】LeetCode //C - 599. Minimum Index Sum of Two Lists。

Made in Code 841

LeetCode599. Minimum Index Sum of Two Lists [C++]

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 theircommon interestwith the...

iuvgood 239

[leetcode] 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 restaurants represented by strings. You need to help them find out their common interest with th

kangbin825的专栏 1960

leetcode 599. Minimum Index Sum of Two Lists | 599. 两个列表的最小索引总和

题目 https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/ 题解 思路和题解一致,用 map 搞定。效率低可能是因为建了两个 map。 import java.util.ArrayList; import java.util.HashMap; import java.util.Map; class Solution { public String[] findRestaurant(String[] list1, Stri

寒泉 5万+

【LeetCode】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 restaurants represented by strings. You need to help them find out their common interest with the l

a342500329a的专栏 410

【Leetcode_easy】599. Minimum Index Sum of Two Lists

problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串。 计算两个的坐标之和,如果与最小坐标和sum相同,那么将这个字符串加入结果res中,如果比sum小,那么sum更新为这个较小值,然后将结果res清空并加入这个字符串。 solution: class Solution { public...

weixin_30588907的博客 102
上一篇: LeetCode 492. Construct the Rectangle
下一篇: LeetCode 453. Minimum Moves to Equal Array Elements
HelloSilicat
博客等级 码龄10年 12粉丝 77原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值