text: (window.webpackJsonp=window.webpackJsonp||[]).push([[29],{103:function(e,t,n){"use strict";n.d(t,"a",(function(){return u})),n.d(t,"b",(function(){return d}));var r=n(0),a=n.n(r);function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){c(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},c=Object.keys(e);for(r=0;r<c.length;r++)n=c[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var c=Object.getOwnPropertySymbols(e);for(r=0;r<c.length;r++)n=c[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var l=a.a.createContext({}),p=function(e){var t=a.a.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},u=function(e){var t=p(e.components);return a.a.createElement(l.Provider,{value:t},e.children)},b={inlineCode:"code",wrapper:function(e){var t=e.children;return a.a.createElement(a.a.Fragment,{},t)}},m=a.a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,c=e.originalType,o=e.parentName,l=s(e,["components","mdxType","originalType","parentName"]),u=p(n),m=r,d=u["".concat(o,".").concat(m)]||u[m]||b[m]||c;return n?a.a.createElement(d,i(i({ref:t},l),{},{components:n})):a.a.createElement(d,i({ref:t},l))}));function d(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var c=n.length,o=new Array(c);o[0]=m;var i={};for(var s in t)hasOwnProperty.call(t,s)&&(i[s]=t[s]);i.originalType=e,i.mdxType="string"==typeof e?e:r,o[1]=i;for(var l=2;l<c;l++)o[l]=n[l];return a.a.createElement.apply(null,o)}return a.a.createElement.apply(null,n)}m.displayName="MDXCreateElement"},83:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return o})),n.d(t,"metadata",(function(){return i})),n.d(t,"rightToc",(function(){return s})),n.d(t,"default",(function(){return p}));var r=n(2),a=n(6),c=(n(0),n(103)),o={id:"twosum",title:"1.\u4e24\u6570\u4e4b\u548c",tags:["\u7b97\u6cd5","\u6570\u636e\u7ed3\u6784","javascript","algorithms","Map"]},i={unversionedId:"leetcode/twosum",id:"leetcode/twosum",isDocsHomePage:!1,title:"1.\u4e24\u6570\u4e4b\u548c",description:"\u4f5c\u4e3a\u529b\u6263\u7684\u7b2c\u4e00\u9898\uff0c\u867d\u7136\u6253\u4e0a easy \u7684\u6807\u7b7e\uff0c\u4f46\u662f\u5bf9\u4e8e\u6ca1\u6709\u7b97\u6cd5\u7ecf\u9a8c\u7684\u540c\u5b66\u6765\u8bf4\uff0c\u8fd8\u662f\u6709\u4e00\u5b9a\u96be\u5ea6\u7684\u3002\u4e24\u6570\u4e4b\u548c\u7ed9\u6211\u4eec\u5e26\u6765\u7684\u5e2e\u52a9\u662f: \u5f53\u6211\u4eec\u5728\u9762\u5bf9\u4e00\u4e9b\u5faa\u73af\u5d4c\u5957\u95ee\u9898\u65f6\uff0c\u9700\u8981\u4ece\u590d\u6742\u7684\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u8df3\u51fa\u6765\uff0c\u5229\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u601d\u60f3\u6765\u6c42\u89e3\u3002",source:"@site/docs/leetcode/twosum.md",permalink:"/blog/docs/leetcode/twosum",editUrl:"https://github.com/zhenbushidashen/callmeboy/tree/master/docs/leetcode/twosum.md",sidebar:"blog",previous:{title:"Style Guide",permalink:"/blog/docs/doc1"},next:{title:"container-with-most-water",permalink:"/blog/docs/leetcode/container-with-most-water"}},s=[],l={rightToc:s};function p(e){var t=e.components,n=Object(a.a)(e,["components"]);return Object(c.b)("wrapper",Object(r.a)({},l,n,{components:t,mdxType:"MDXLayout"}),Object(c.b)("div",{className:"admonition admonition-tip alert alert--success"},Object(c.b)("div",Object(r.a)({parentName:"div"},{className:"admonition-heading"}),Object(c.b)("h5",{parentName:"div"},Object(c.b)("span",Object(r.a)({parentName:"h5"},{className:"admonition-icon"}),Object(c.b)("svg",Object(r.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"12",height:"16",viewBox:"0 0 12 16"}),Object(c.b)("path",Object(r.a)({parentName:"svg"},{fillRule:"evenodd",d:"M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"})))),"tip")),Object(c.b)("div",Object(r.a)({parentName:"div"},{className:"admonition-content"}),Object(c.b)("p",{parentName:"div"},"\u4f5c\u4e3a\u529b\u6263\u7684\u7b2c\u4e00\u9898\uff0c\u867d\u7136\u6253\u4e0a ",Object(c.b)("inlineCode",{parentName:"p"},"easy")," \u7684\u6807\u7b7e\uff0c\u4f46\u662f\u5bf9\u4e8e\u6ca1\u6709\u7b97\u6cd5\u7ecf\u9a8c\u7684\u540c\u5b66\u6765\u8bf4\uff0c\u8fd8\u662f\u6709\u4e00\u5b9a\u96be\u5ea6\u7684\u3002\u4e24\u6570\u4e4b\u548c\u7ed9\u6211\u4eec\u5e26\u6765\u7684\u5e2e\u52a9\u662f: \u5f53\u6211\u4eec\u5728\u9762\u5bf9\u4e00\u4e9b\u5faa\u73af\u5d4c\u5957\u95ee\u9898\u65f6\uff0c\u9700\u8981\u4ece\u590d\u6742\u7684\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u8df3\u51fa\u6765\uff0c\u5229\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u601d\u60f3\u6765\u6c42\u89e3\u3002"))),Object(c.b)("h4",{id:"\u9898\u76ee"},"\u9898\u76ee"),Object(c.b)("p",null,"\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums\xa0\u548c\u4e00\u4e2a\u76ee\u6807\u503c target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa\u548c\u4e3a\u76ee\u6807\u503c\u7684\u90a3\xa0\u4e24\u4e2a\xa0\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u4ed6\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002"),Object(c.b)("p",null,"\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u6570\u7ec4\u4e2d\u540c\u4e00\u4e2a\u5143\u7d20\u4e0d\u80fd\u4f7f\u7528\u4e24\u904d"),Object(c.b)("pre",null,Object(c.b)("code",Object(r.a)({parentName:"pre"},{}),"\u7ed9\u5b9a nums = [2, 7, 11, 15], target = 9\n\n\u56e0\u4e3a nums[0] + nums[1] = 2 + 7 = 9\n\u6240\u4ee5\u8fd4\u56de [0, 1]\n\n")),Object(c.b)("p",null,"\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09"),Object(c.b)("h4",{id:"\u9898\u89e3"},"\u9898\u89e3:"),Object(c.b)("pre",null,Object(c.b)("code",Object(r.a)({parentName:"pre"},{className:"language-javascript"}),"function twoSum(nums, target) {\n  const cacheMap = new Map\n  for (let i = 0,len = nums.length; i < len i++) {\n    if (cacheMap.has(nums[i])) {\n      return [ cacheMap.get(nums[i]), i ]\n    }\n    cacheMap.set(target - nums[i], i)\n  }\n  return []\n}\n\n\n")))}p.isMDXComponent=!0}}]);