{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "b83f11e3c3994e3c9d5d2668998c6d47",
  "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": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic List<List<Integer>> fourSum(int[] nums, int target) {\n\t\tlong l_target = target;\n\t\tArrays.sort(nums);\n\t\tList<List<Integer>> results = new ArrayList<>();\n\t\tint N = nums.length;\n\t\tfor (int i = 0; i < N - 3; i++) {\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\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\tif (k > j + 1 && nums[k] == nums[k - 1])\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\twhile (k < l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0) {\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\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\tList<Integer> item = new ArrayList<>();\n\t\t\t\t\t\titem.add(nums[i]);\n\t\t\t\t\t\titem.add(nums[j]);\n\t\t\t\t\t\titem.add(nums[k]);\n\t\t\t\t\t\titem.add(nums[l]);\n\t\t\t\t\t\tresults.add(item);\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": "java"
  },
  "node_id": "dailycode-e7dd2c02e6b94b02a2cba6cb32b0c0dc",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600469919"
}