|
| 1 | +{ |
| 2 | + "problem_name": "three_sum", |
| 3 | + "solution_class_name": "Solution", |
| 4 | + "problem_number": "15", |
| 5 | + "problem_title": "3Sum", |
| 6 | + "difficulty": "Medium", |
| 7 | + "topics": "Array, Two Pointers, Sorting", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.\n\nNotice that the solution set must not contain duplicate triplets.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "```\nInput: nums = [-1,0,1,2,-1,-4]\nOutput: [[-1,-1,2],[-1,0,1]]\n```\n**Explanation:** \nnums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.\nnums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.\nnums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.\nThe distinct triplets are [-1,0,1] and [-1,-1,2].\nNotice that the order of the output and the order of the triplets does not matter." |
| 13 | + }, |
| 14 | + { |
| 15 | + "content": "```\nInput: nums = [0,1,1]\nOutput: []\n```\n**Explanation:** The only possible triplet does not sum up to 0." |
| 16 | + }, |
| 17 | + { |
| 18 | + "content": "```\nInput: nums = [0,0,0]\nOutput: [[0,0,0]]\n```\n**Explanation:** The only possible triplet sums up to 0." |
| 19 | + } |
| 20 | + ], |
| 21 | + "readme_constraints": "- 3 <= nums.length <= 3000\n- -10^5 <= nums[i] <= 10^5", |
| 22 | + "readme_additional": "", |
| 23 | + "solution_imports": "", |
| 24 | + "solution_methods": [ |
| 25 | + { |
| 26 | + "name": "three_sum", |
| 27 | + "parameters": "nums: list[int]", |
| 28 | + "return_type": "list[list[int]]", |
| 29 | + "dummy_return": "[]" |
| 30 | + } |
| 31 | + ], |
| 32 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution", |
| 33 | + "test_class_name": "ThreeSum", |
| 34 | + "test_helper_methods": [ |
| 35 | + { "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" } |
| 36 | + ], |
| 37 | + "test_methods": [ |
| 38 | + { |
| 39 | + "name": "test_three_sum", |
| 40 | + "parametrize": "nums, expected", |
| 41 | + "parametrize_typed": "nums: list[int], expected: list[list[int]]", |
| 42 | + "test_cases": "[([-1, 0, 1, 2, -1, -4], [[-1, -1, 2], [-1, 0, 1]]), ([0, 1, 1], []), ([0, 0, 0], [[0, 0, 0]]), ([-1, 0, 1], [[-1, 0, 1]]), ([1, 2, -2, -1], []), ([-2, 0, 1, 1, 2], [[-2, 0, 2], [-2, 1, 1]])]", |
| 43 | + "body": "result = self.solution.three_sum(nums)\n# Sort both result and expected for comparison since order doesn't matter\nresult_sorted = [sorted(triplet) for triplet in result]\nexpected_sorted = [sorted(triplet) for triplet in expected]\nresult_sorted.sort()\nexpected_sorted.sort()\nassert result_sorted == expected_sorted" |
| 44 | + } |
| 45 | + ], |
| 46 | + "playground_imports": "from solution import Solution", |
| 47 | + "playground_test_case": "# Example test case\nnums = [-1, 0, 1, 2, -1, -4]\nexpected = [[-1, -1, 2], [-1, 0, 1]]", |
| 48 | + "playground_execution": "result = Solution().three_sum(nums)\nresult", |
| 49 | + "playground_assertion": "# Sort for comparison since order doesn't matter\nresult_sorted = [sorted(triplet) for triplet in result]\nexpected_sorted = [sorted(triplet) for triplet in expected]\nresult_sorted.sort()\nexpected_sorted.sort()\nassert result_sorted == expected_sorted" |
| 50 | +} |
0 commit comments