solution.json 2.8 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": "019e45cad3ba412aa84ebbc18cba97aa",
  "keywords": "数组,双指针,排序",
  "title": "四数之和",
  "desc": [
    {
      "content": "\n<p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= nums.length <= 200</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></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "class Solution\n{\npublic:\n\tvector<vector<int>> fourSum(vector<int> &nums, int target)\n\t{\n\t\tlong long l_target = target;\n\t\tsort(nums.begin(), nums.end());\n\t\tvector<vector<int>> results;\n\t\tint N = nums.size();\n\t\tfor (int i = 0; i < N - 3; i++)\n\t\t{\n\t\t\tif (i > 0 && nums[i] == nums[i - 1])\n\t\t\t\tcontinue;\n\t\t\tfor (int j = i + 1; j < N - 2; j++)\n\t\t\t{\n\t\t\t\tif (j > i + 1 && nums[j] == nums[j - 1])\n\t\t\t\t\tcontinue;\n\t\t\t\tfor (int k = j + 1, l = N - 1; k < l; k++)\n\t\t\t\t{\n\t\t\t\t\tif (k > j + 1 && nums[k] == nums[k - 1])\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\twhile (k < l &&\n\t\t\t\t\t\t   (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tl--;\n\t\t\t\t\t}\n\t\t\t\t\tif (k >= l)\n\t\t\t\t\t{\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tresults.emplace_back(\n\t\t\t\t\t\t\tvector<int>({nums[i], nums[j], nums[k], nums[l]}));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn results;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-015937d27ef34a46b5ef9f61bde201e1",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600469919"
}