diff --git a/animation-simulation/数组篇/leetcode1两数之和.md b/animation-simulation/数组篇/leetcode1两数之和.md index e17e1bd..43be63b 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[nums[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); + } +}; +``` +