From 5e6ded112679e1aa2bd90a4074e9ac60295a2031 Mon Sep 17 00:00:00 2001 From: hansliao2015 Date: Wed, 11 Sep 2024 20:12:20 +0800 Subject: [PATCH 1/2] o(n^2) solution --- two_sum.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two_sum.cpp b/two_sum.cpp index ac6a162..26f3a74 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 (target == nums[i] + nums[j]) { // found pair return {i, j}; } From 5360602c2aab54a2adf234ae7a6f723bb87204cf Mon Sep 17 00:00:00 2001 From: hansliao2015 Date: Wed, 11 Sep 2024 20:18:35 +0800 Subject: [PATCH 2/2] second modification --- two_sum.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two_sum.cpp b/two_sum.cpp index 26f3a74..bb875e6 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 (target == nums[i] + nums[j]) { + if (i != j && target == nums[i] + nums[j]) { // found pair return {i, j}; }