We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:
输入:nums = [-1,0,1,2,-1,-4] 输出:[[-1,-1,2],[-1,0,1]]
示例 2:
输入:nums = [] 输出:[]
示例 3:
输入:nums = [0] 输出:[]
提示: 0 <= nums.length <= 3000 -105 <= nums[i] <= 105
The text was updated successfully, but these errors were encountered:
function threeSum(nums: number[]): number[][] { const result: number[][] = []; const len = nums.length; if (len < 3) return result; nums.sort((a, b) => a - b); for (let i = 0; i < len - 2; i++) { if (nums[i] > 0) break; if (i > 0 && nums[i] === nums[i - 1]) continue; let l = i + 1; let r = len - 1; while (l < r) { const sum = nums[i] + nums[l] + nums[r]; if (sum === 0) { result.push([nums[i], nums[l], nums[r]]); while (l < r && nums[l] === nums[l + 1]) l++; while (l < r && nums[r] === nums[r - 1]) r--; l++; r--; } else if (sum < 0) { l++; } else { r--; } } } return result; }
Sorry, something went wrong.
No branches or pull requests
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:
示例 2:
示例 3:
提示:
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
The text was updated successfully, but these errors were encountered: