{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "7f235698f48c4268b1712d39303f1fef",
  "keywords": "数组,双指针,排序",
  "title": "三数之和",
  "desc": [
    {
      "content": "\n<p>给你一个包含 <code>n</code> 个整数的数组 <code>nums</code>，判断 <code>nums</code> 中是否存在三个元素 <em>a，b，c ，</em>使得 <em>a + b + c = </em>0 ？请你找出所有和为 <code>0</code> 且不重复的三元组。</p><p><strong>注意：</strong>答案中不可以包含重复的三元组。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums = [-1,0,1,2,-1,-4]<strong><br />输出：</strong>[[-1,-1,2],[-1,0,1]]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums = []<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>nums = [0]<strong><br />输出：</strong>[]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>0 <= nums.length <= 3000</code></li>\t<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</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>> threeSum(int[] nums) {\n\t\tArrays.sort(nums);\n\t\tList<List<Integer>> result = new ArrayList<>();\n\t\tint cur, left, right;\n\t\tcur = 0;\n\t\twhile (cur < nums.length) {\n\t\t\tif (nums[cur] > 0)\n\t\t\t\tbreak;\n\t\t\tleft = cur + 1;\n\t\t\tright = nums.length - 1;\n\t\t\twhile (left < right) {\n\t\t\t\tint n = nums[cur] + nums[left] + nums[right];\n\t\t\t\tif (n == 0) {\n\t\t\t\t\tList<Integer> r = new ArrayList<Integer>();\n\t\t\t\t\tr.add(nums[cur]);\n\t\t\t\t\tr.add(nums[left]);\n\t\t\t\t\tr.add(nums[right]);\n\t\t\t\t\tresult.add(r);\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t\tt = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t} else if (n > 0) {\n\t\t\t\t\tint t = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t} else {\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t}\n\t\t\t}\n\t\t\tint t = cur + 1;\n\t\t\twhile (t < nums.length && nums[t] == nums[cur])\n\t\t\t\tt++;\n\t\t\tcur = t;\n\t\t}\n\t\treturn result;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-7c1a5ad714c441d7a0ccdee3c819e4c6",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470113"
}