From 1651e74bfa8eb1980814a744280be98d33d9c390 Mon Sep 17 00:00:00 2001 From: xingyou wu <3050128610@qq.com> Date: Thu, 14 Aug 2025 23:37:03 +0800 Subject: [PATCH] =?UTF-8?q?15.=20=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test006.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test006.cpp diff --git a/test006.cpp b/test006.cpp new file mode 100644 index 0000000..e3109f3 --- /dev/null +++ b/test006.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + // 主要问题还是去重复 + int n = nums.size(); + sort(nums.begin(), nums.end()); + vector> ret; + int preid = -1; + for(int i = 0; i < n; i++){ + if(preid != -1 && nums[i] == nums[preid]) continue; + int l = i + 1, r = n - 1; + while(l < r){ + int sum = nums[i] + nums[l] + nums[r]; + if(sum == 0){ + ret.push_back({nums[l], nums[r], nums[i]}); + int tmp = nums[l]; + while(l < r && nums[l] == tmp) l++; + preid = i; + }else if(sum > 0){ + r--; + }else{ + l++; + } + } + } + return ret; + } +}; \ No newline at end of file