[{"data":1,"prerenderedAt":182},["ShallowReactive",2],{"content:\u002Fposts\u002F2024\u002Fleetcode-two-sum":3,"surround:\u002Fposts\u002F2024\u002Fleetcode-two-sum":171},{"id":4,"title":5,"body":6,"canonical":149,"categories":150,"date":152,"description":149,"draft":153,"extension":154,"image":155,"meta":156,"navigation":157,"path":158,"permalink":149,"readingTime":159,"recommend":149,"references":149,"seo":164,"sitemap":165,"stem":166,"tags":167,"type":149,"updated":152,"__hash__":170},"content\u002Fposts\u002F2024\u002Fleetcode-two-sum.md","leetcode 两数之和",{"type":7,"value":8,"toc":136},"minimark",[9,13,20,23,26,29,34,58,62,71,75,82,86,93,104,108,114,122,126],[10,11,12],"h2",{"id":12},"题目描述",[14,15,16],"p",{},[17,18,19],"code",{"code":19},"简单题",[14,21,22],{},"给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。",[14,24,25],{},"你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。",[14,27,28],{},"你可以按任意顺序返回答案。",[30,31,33],"h3",{"id":32},"示例-1","示例 1：",[14,35,36,37,41,42,45,46,49,50,53,54,57],{},"输入：nums = ",[38,39,40],"span",{},"2,7,11,15",", target = 9\n输出：",[38,43,44],{},"0,1","\n解释：因为 nums",[38,47,48],{},"0"," + nums",[38,51,52],{},"1"," == 9 ，返回 ",[38,55,56],{},"0, 1"," 。",[30,59,61],{"id":60},"示例-2","示例 2：",[14,63,36,64,67,68],{},[38,65,66],{},"3,2,4",", target = 6\n输出：",[38,69,70],{},"1,2",[30,72,74],{"id":73},"示例-3","示例 3：",[14,76,36,77,67,80],{},[38,78,79],{},"3,3",[38,81,44],{},[30,83,85],{"id":84},"提示","提示：",[14,87,88,89,92],{},"2 \u003C= nums.length \u003C= 104\n-109 \u003C= nums",[38,90,91],{},"i"," \u003C= 109\n-109 \u003C= target \u003C= 109\n只会存在一个有效答案",[14,94,95,96,103],{},"**跳转至当前题目 ",[97,98,102],"a",{"href":99,"rel":100},"https:\u002F\u002Fleetcode.cn\u002Fproblems\u002Ftwo-sum\u002Fdescription\u002F",[101],"nofollow","leetcode"," **",[10,105,107],{"id":106},"解题思路","解题思路：",[14,109,110,111,113],{},"利用 Map 存储数组中的元素和索引，遍历数组，对于每个元素，计算它的补数（target - nums",[38,112,91],{},"），如果 Map 中存在这个补数，则找到了两个元素的和，返回它们的索引；否则，将当前元素和它的索引存入 Map。",[14,115,116,119],{},[17,117,118],{"code":118},"时间复杂度：O(n)",[17,120,121],{"code":121},"空间复杂度：O(n)",[10,123,125],{"id":124},"代码如下","代码如下：",[127,128,134],"pre",{"className":129,"code":131,"language":132,"meta":133},[130],"language-js","function towSum(nums, target) {\n    const map = new Map()\n\n    for (let i = 0; i \u003C nums.length; i++) {\n        const complement = target - nums[i]\n        if (map.has(complement)) {\n            return [map.get(complement), i]\n        }\n        else {\n            map.set(nums[i], i)\n        }\n    }\n    return []\n}\n","js","",[17,135,131],{"__ignoreMap":133},{"title":133,"searchDepth":137,"depth":137,"links":138},4,[139,147,148],{"id":12,"depth":140,"text":12,"children":141},2,[142,144,145,146],{"id":32,"depth":143,"text":33},3,{"id":60,"depth":143,"text":61},{"id":73,"depth":143,"text":74},{"id":84,"depth":143,"text":85},{"id":106,"depth":140,"text":107},{"id":124,"depth":140,"text":125},null,[151],"代码","2024-05-18 11:00:25",false,"md","https:\u002F\u002Fbitmc.uno\u002Fpicgo\u002F679adc95e822c-78.webp",{},true,"\u002Fposts\u002F2024\u002Fleetcode-two-sum",{"text":160,"minutes":161,"time":162,"words":163},"2 min read",1.595,95700,319,{"title":5,"description":149},{"loc":158},"posts\u002F2024\u002Fleetcode-two-sum",[168,169,102],"JavaScript","算法","CB54QHihGRuUy_eJ2BSn0SllclOnSIhwJrP1hC42gzw",[172,177],{"title":173,"path":174,"stem":175,"date":176,"type":149,"children":-1},"ECharts 在 vue2 中的实践","\u002Fposts\u002F2024\u002Fchart-echarts-in-vue2-component","posts\u002F2024\u002Fchart-echarts-in-vue2-component","2024-04-16 20:02:27",{"title":178,"path":179,"stem":180,"date":181,"type":149,"children":-1},"Nuxt3 请求数据","\u002Fposts\u002F2024\u002Fnuxt3-fetch-data","posts\u002F2024\u002Fnuxt3-fetch-data","2024-06-05 22:26:42",1789642591981]