{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "3b908215610d47689b9c2463e30ba9b6",
  "keywords": "数组,哈希表",
  "title": "两数之和",
  "desc": [
    {
      "content": "\n<p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>，请你在该数组中找出 <strong>和为目标值</strong> 的那 <strong>两个</strong> 整数，并返回它们的数组下标。</p><p>你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。</p><p>你可以按任意顺序返回答案。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums = [2,7,11,15], target = 9<strong><br />输出：</strong>[0,1]<strong><br />解释：</strong>因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums = [3,2,4], target = 6<strong><br />输出：</strong>[1,2]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>nums = [3,3], target = 6<strong><br />输出：</strong>[0,1]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>2 <= nums.length <= 10<sup>3</sup></code></li>\t<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>\t<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>\t<li><strong>只会存在一个有效答案</strong></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <unordered_map>\nclass Solution\n{\npublic:\n\tvector<int> twoSum(vector<int> &nums, int target)\n\t{\n\t\tstd::unordered_map<int, int> hset;\n\t\tvector<int> r;\n\t\tfor (int i = 0; i < nums.size(); ++i)\n\t\t{\n\t\t\tint c = target - nums[i];\n\t\t\tauto iter = hset.find(c);\n\t\t\tif (iter != hset.end() && iter->second != i)\n\t\t\t{\n\t\t\t\tr.push_back(i);\n\t\t\t\tr.push_back(iter->second);\n\t\t\t\treturn r;\n\t\t\t}\n\t\t\thset.insert(std::make_pair(nums[i], i));\n\t\t}\n\t\treturn r;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-cfd7bb9455e644749ffc5204d9012feb",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470217"
}