LEETCODE ALGORITHM:15. 3Sum
题目
Given an integer array nums, return all the triplets
[nums[i], nums[j], nums[k]]
such thati != j
,i != k
, andj != k
, andnums[i] + nums[j] + nums[k] == 0
.Notice that the solution set must not contain duplicate triplets.
Example 1:
1
2Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]Example 2:
1
2Input: nums = []
Output: []Example 3:
1
2Input: nums = [0]
Output: []Constraints:
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
题解
i,j,k分别代表排序后数组里三个数的索引号
因为要避免重复,一样的数组不要,所以如果任何一个位置前后两次选择的数一致就要跳过
第三个数的指针倒叙来找
1 | class Solution { |