Browser tab content: Problem List Register or Sign in Premium Debugging... Run Submit…
- User
Browser tab content: Problem List Register or Sign in Premium Debugging... Run Submit Description Editorial Editorial Solutions Solutions Submissions Submissions Code Testcase Test Result Test Result 15. 3Sum Medium Topics Companies Hint
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.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0.
Constraints:
3 <= nums.length <= 3000 -105 <= nums[i] <= 105 Accepted 4.1M Submissions 11.5M Acceptance Rate 35.7% Topics Array Two Pointers Sorting Companies Hint 1 So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand! Hint 2 For the two-sum problem, if we fix one of the numbers, say x, we have to scan the entire array to find the next number y, which is value - x where value is the input parameter. Can we change our array somehow so that this search becomes faster? Hint 3 The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search? Similar Questions Two Sum Easy 3Sum Closest Medium 4Sum Medium 3Sum Smaller Medium Number of Arithmetic Triplets Easy Minimum Sum of Mountain Triplets I Easy Minimum Sum of Mountain Triplets II Medium Discussion (484) Choose a type Preview Comment 💡 Discussion Rules
-
Please don't post any solutions in this discussion.
-
The problem discussion is for asking questions about the problem or for sharing tips - anything except for solutions.
-
If you'd like to share your solution for feedback and ideas, please head to the solutions tab and post it there.
Sort by:Best No comments yet. 1 2 3 4 5 6 49 Copyright ©️ 2024 LeetCode All rights reserved 31.7K 484 556 Online C++ Auto 1 2 3 4 5 6 class Solution { public: vector<vector> threeSum(vector& nums) { } }; Saved Ln 4, Col 9 You need to Login / Sign up to run or submit Case 1 Case 2 Case 3 nums = [-1,0,1,2,-1,-4] 1 2 3 [-1,0,1,2,-1,-4] [0,1,1] [0,0,0] Source
Problem List Tag
-
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.