From 067c3339a216315f209a5ba4680af70103b08f55 Mon Sep 17 00:00:00 2001 From: colorbox Date: Mon, 11 Mar 2024 03:30:39 +0900 Subject: [PATCH 1/2] 1. Two Sum --- 1/1.cpp | 19 +++++++++++++++++++ 1/2.cpp | 18 ++++++++++++++++++ 1/3.cpp | 17 +++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 1/1.cpp create mode 100644 1/2.cpp create mode 100644 1/3.cpp diff --git a/1/1.cpp b/1/1.cpp new file mode 100644 index 0000000..88c6881 --- /dev/null +++ b/1/1.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + map numMap; + + int index = 0; + int currentTargetNum = 0; + for(; index < nums.size(); index++){ + currentTargetNum = target - nums[index]; + if(numMap.find(currentTargetNum) != numMap.end() && index != numMap[currentTargetNum]){ + break; + } + + numMap[nums[index]] = index; + } + + return { index, numMap[currentTargetNum] }; + } +}; diff --git a/1/2.cpp b/1/2.cpp new file mode 100644 index 0000000..0268b2e --- /dev/null +++ b/1/2.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + map numMap; + + int index, currentTargetNum; + for(index = 0; index < nums.size(); index++){ + currentTargetNum = target - nums[index]; + if(numMap.find(currentTargetNum) != numMap.end()){ + break; + } + + numMap[nums[index]] = index; + } + + return { index, numMap[currentTargetNum] }; + } +}; diff --git a/1/3.cpp b/1/3.cpp new file mode 100644 index 0000000..2623c9b --- /dev/null +++ b/1/3.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + map numMap; + int currentOther, index; + + for(index = 0; index < nums.size(); index++){ + currentOther = target - nums[index]; + if(numMap.find(currentOther) != numMap.end()){ + break; + } + numMap[nums[index]] = index; + } + + return { index, numMap[currentOther] }; + } +}; From b2b3a77546a7663d2bcc7a620a5e51b2661346a5 Mon Sep 17 00:00:00 2001 From: colorbox Date: Tue, 12 Mar 2024 01:48:49 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E5=BE=8C=E3=81=AE=E6=94=B9=E8=89=AF=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1/4.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1/4.cpp diff --git a/1/4.cpp b/1/4.cpp new file mode 100644 index 0000000..8752b21 --- /dev/null +++ b/1/4.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + map numMap; + for(int index = 0; index < nums.size(); index++){ + int numToFind = target - nums[index]; + if(numMap.find(numToFind) != numMap.end()){ + return { index, numMap[numToFind] }; + } + numMap[nums[index]] = index; + } + + return {}; + } +};