From 933bdd62963a08b1ddce7233c46059dbc9a8554b Mon Sep 17 00:00:00 2001 From: xingyou wu <3050128610@qq.com> Date: Fri, 8 Aug 2025 16:06:30 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=B8=A4=E6=95=B0=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test001.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test001.cpp diff --git a/test001.cpp b/test001.cpp new file mode 100644 index 0000000..a05288a --- /dev/null +++ b/test001.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + int n = nums.size(); + vector idxs; + for(int i = 0; i < n; i++) idxs.push_back(i); + + // Can¡®t be sorted directly + sort(idxs.begin(), idxs.end(), [&nums](int i, int j){ + return nums[i] < nums[j]; + }); + int l = 0, r = n - 1; + vector res; + while(l < r){ + int sum = nums[idxs[l]] + nums[idxs[r]]; + if(sum == target){ + res.push_back(idxs[l]); + res.push_back(idxs[r]); + break; + }else if(sum > target){ + r--; + }else{ + l++; + } + } + return res; + } +}; \ No newline at end of file