From 783b30e245826ff27f37be60761b6cceb117a79c Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Sep 2024 20:13:48 +0800 Subject: [PATCH 1/2] 123 --- two_sum.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/two_sum.cpp b/two_sum.cpp index ac6a162..58eddf9 100644 --- a/two_sum.cpp +++ b/two_sum.cpp @@ -20,4 +20,5 @@ int main() { vector v = {2,4,7,9,11,19}; vector ans = twoSum(v, 15); cout<< "The indices of the two numbers that add up to target: " << ans[0] << ", " << ans[1]; + } \ No newline at end of file From bbd9b28bf83588d39d97ff06fc748801e858e61b Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Sep 2024 20:27:21 +0800 Subject: [PATCH 2/2] o(n^2) solution --- two_sum.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/two_sum.cpp b/two_sum.cpp index 58eddf9..2d0c9c2 100644 --- a/two_sum.cpp +++ b/two_sum.cpp @@ -7,7 +7,7 @@ vector twoSum(vector& nums, int target) { int n = nums.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { - if (/* TODO */) { + if (i != j && num[i]+num[j] == target) { // found pair return {i, j}; } @@ -20,5 +20,5 @@ int main() { vector v = {2,4,7,9,11,19}; vector ans = twoSum(v, 15); cout<< "The indices of the two numbers that add up to target: " << ans[0] << ", " << ans[1]; - + } \ No newline at end of file