solution.json 2.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "82aaf8b8a58b48538f12023fb9c12101",
  "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": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic int[] twoSum(int[] nums, int target) {\n\t\tMap<Integer, Integer> cache = new HashMap<>();\n\t\tfor (int i = 0; i < nums.length; i++) {\n\t\t\tint distance = target - nums[i];\n\t\t\tif (cache.containsKey(distance)) {\n\t\t\t\treturn new int[] { cache.get(distance), i };\n\t\t\t} else {\n\t\t\t\tcache.put(nums[i], i);\n\t\t\t}\n\t\t}\n\t\treturn new int[] {};\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-b99fbefc0fd34ea9b5a17db12f45f1c5",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470217"
}