From 4e30ea7ae7b9c4fc4d55aa02ac833ad3fd601321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=A5=96=E6=BA=90?= <15692551+liang-zuyuan@user.noreply.gitee.com> Date: Tue, 16 Sep 2025 22:53:28 +0800 Subject: [PATCH] =?UTF-8?q?20250916=E7=AC=AC=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\346\242\201\347\245\226\346\272\220.cpp" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "exercise/LC0001_\346\242\201\347\245\226\346\272\220.cpp" diff --git "a/exercise/LC0001_\346\242\201\347\245\226\346\272\220.cpp" "b/exercise/LC0001_\346\242\201\347\245\226\346\272\220.cpp" new file mode 100644 index 0000000..678d6c5 --- /dev/null +++ "b/exercise/LC0001_\346\242\201\347\245\226\346\272\220.cpp" @@ -0,0 +1,25 @@ +#include +using namespace std; + +// 【题目】力扣001. 两数之和 +// 【难度】简单 +// 【提交】https://leetcode.cn/problems/two-sum/submissions/663377045/ +// 【标签】数组;哈希表 +class Solution { +public: + std::vector twoSum(std::vector& nums, int target) { + std::unordered_map hash_map; + + for (int i = 0; i < nums.size(); ++i) { + int complement = target - nums[i]; + + if (hash_map.find(complement) != hash_map.end()) { + return {hash_map[complement], i}; + } + + hash_map[nums[i]] = i; + } + + return {}; + } +}; \ No newline at end of file -- Gitee