From 9a8b4f47a4f674cdb69d49477f8ccbfc66285d2c Mon Sep 17 00:00:00 2001 From: lucifer Date: Mon, 26 Apr 2021 14:44:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=20leetcode1=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../数组篇/leetcode1两数之和.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/animation-simulation/数组篇/leetcode1两数之和.md b/animation-simulation/数组篇/leetcode1两数之和.md index e17e1bd..54c38ea 100644 --- a/animation-simulation/数组篇/leetcode1两数之和.md +++ b/animation-simulation/数组篇/leetcode1两数之和.md @@ -69,6 +69,8 @@ class Solution { **题目代码:** +Java Code: + ```java class Solution { public int[] twoSum(int[] nums, int target) { @@ -88,5 +90,37 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + vector twoSum(vector& nums, int target) { + unordered_map m; + for (int i = 0; i < nums.size(); ++i) { + int t = target - nums[i]; + if (m.count(t)) return { m[t], i }; + m[A[i]] = i; + } + return {}; + } +}; +``` + +JS Code: + +```js +const twoSum = function (nums, target) { + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + const diff = target - nums[i]; + if (map.has(diff)) { + return [map.get(diff), i]; + } + map.set(nums[i], i); + } +}; +``` + From 5908cad71b0df600d29f6ff0bf774c607fd273aa Mon Sep 17 00:00:00 2001 From: lucifer Date: Mon, 26 Apr 2021 14:55:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=20leetcode1=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animation-simulation/数组篇/leetcode1两数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/animation-simulation/数组篇/leetcode1两数之和.md b/animation-simulation/数组篇/leetcode1两数之和.md index 54c38ea..43be63b 100644 --- a/animation-simulation/数组篇/leetcode1两数之和.md +++ b/animation-simulation/数组篇/leetcode1两数之和.md @@ -100,7 +100,7 @@ public: for (int i = 0; i < nums.size(); ++i) { int t = target - nums[i]; if (m.count(t)) return { m[t], i }; - m[A[i]] = i; + m[nums[i]] = i; } return {}; }